drupal/modules/php/php.test

108 lines
3.8 KiB
Plaintext

<?php
// $Id$
/**
* Base PHP test case class.
*/
class PHPTestCase extends DrupalWebTestCase {
protected $php_code_format;
function setUp() {
parent::setUp('php');
// Create and login admin user.
$admin_user = $this->drupalCreateUser(array('administer filters'));
$this->drupalLogin($admin_user);
// Confirm that the PHP code text format was inserted as the newest format
// on the site.
$newest_format_id = db_query("SELECT MAX(format) FROM {filter_format}")->fetchField();
$newest_format = filter_format_load($newest_format_id);
$this->assertEqual($newest_format->name, 'PHP code', t('PHP code text format was created.'));
// Store the format ID of the PHP code text format for later use.
$this->php_code_format = $newest_format_id;
}
/**
* Create a test node with PHP code in the body.
*
* @return stdObject Node object.
*/
function createNodeWithCode() {
return $this->drupalCreateNode(array('body' => array(FIELD_LANGUAGE_NONE => array(array('value' => '<?php print "SimpleTest PHP was executed!"; ?>')))));
}
}
/**
* Tests to make sure the PHP filter actually evaluates PHP code when used.
*/
class PHPFilterTestCase extends PHPTestCase {
public static function getInfo() {
return array(
'name' => 'PHP filter functionality',
'description' => 'Make sure that PHP filter properly evaluates PHP code when enabled.',
'group' => 'PHP',
);
}
/**
* Make sure that the PHP filter evaluates PHP code when used.
*/
function testPHPFilter() {
// Log in as a user with permission to use the PHP code text format.
$php_code_permission = filter_permission_name(filter_format_load($this->php_code_format));
$web_user = $this->drupalCreateUser(array('access content', 'create page content', 'edit own page content', $php_code_permission));
$this->drupalLogin($web_user);
// Create a node with PHP code in it.
$node = $this->createNodeWithCode();
// Make sure that the PHP code shows up as text.
$this->drupalGet('node/' . $node->nid);
$this->assertText('print', t('PHP code is displayed.'));
// Change filter to PHP filter and see that PHP code is evaluated.
$edit = array();
$langcode = FIELD_LANGUAGE_NONE;
$edit["body[$langcode][0][value_format]"] = $this->php_code_format;
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), t('PHP code filter turned on.'));
// Make sure that the PHP code shows up as text.
$this->assertNoText('print', t('PHP code isn\'t displayed.'));
$this->assertText('SimpleTest PHP was executed!', t('PHP code has been evaluated.'));
}
}
/**
* Tests to make sure access to the PHP filter is properly restricted.
*/
class PHPAccessTestCase extends PHPTestCase {
public static function getInfo() {
return array(
'name' => 'PHP filter access check',
'description' => 'Make sure that users who don\'t have access to the PHP filter can\'t see it.',
'group' => 'PHP',
);
}
/**
* Make sure that user can't use the PHP filter when not given access.
*/
function testNoPrivileges() {
// Create node with PHP filter enabled.
$web_user = $this->drupalCreateUser(array('access content', 'create page content', 'edit own page content'));
$this->drupalLogin($web_user);
$node = $this->createNodeWithCode();
// Make sure that the PHP code shows up as text.
$this->drupalGet('node/' . $node->nid);
$this->assertText('print', t('PHP code is displayed.'));
// Make sure that user doesn't have access to filter.
$this->drupalGet('node/' . $node->nid . '/edit');
$this->assertNoRaw('<option value="' . $this->php_code_format . '">', t('PHP code format not available.'));
}
}