| 1 |
<?php
|
| 2 |
// $Id: php.test,v 1.17 2009/09/20 07:32:18 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Base PHP test case class.
|
| 6 |
*/
|
| 7 |
class PHPTestCase extends DrupalWebTestCase {
|
| 8 |
protected $php_code_format;
|
| 9 |
|
| 10 |
function setUp() {
|
| 11 |
parent::setUp('php');
|
| 12 |
|
| 13 |
// Create and login admin user.
|
| 14 |
$admin_user = $this->drupalCreateUser(array('administer filters'));
|
| 15 |
$this->drupalLogin($admin_user);
|
| 16 |
|
| 17 |
// Confirm that the PHP code text format was inserted as the newest format
|
| 18 |
// on the site.
|
| 19 |
$newest_format_id = db_query("SELECT MAX(format) FROM {filter_format}")->fetchField();
|
| 20 |
$newest_format = filter_format_load($newest_format_id);
|
| 21 |
$this->assertEqual($newest_format->name, 'PHP code', t('PHP code text format was created.'));
|
| 22 |
|
| 23 |
// Store the format ID of the PHP code text format for later use.
|
| 24 |
$this->php_code_format = $newest_format_id;
|
| 25 |
}
|
| 26 |
|
| 27 |
/**
|
| 28 |
* Create a test node with PHP code in the body.
|
| 29 |
*
|
| 30 |
* @return stdObject Node object.
|
| 31 |
*/
|
| 32 |
function createNodeWithCode() {
|
| 33 |
return $this->drupalCreateNode(array('body' => array(FIELD_LANGUAGE_NONE => array(array('value' => '<?php print "SimpleTest PHP was executed!"; ?>')))));
|
| 34 |
}
|
| 35 |
}
|
| 36 |
|
| 37 |
/**
|
| 38 |
* Tests to make sure the PHP filter actually evaluates PHP code when used.
|
| 39 |
*/
|
| 40 |
class PHPFilterTestCase extends PHPTestCase {
|
| 41 |
public static function getInfo() {
|
| 42 |
return array(
|
| 43 |
'name' => 'PHP filter functionality',
|
| 44 |
'description' => 'Make sure that PHP filter properly evaluates PHP code when enabled.',
|
| 45 |
'group' => 'PHP',
|
| 46 |
);
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Make sure that the PHP filter evaluates PHP code when used.
|
| 51 |
*/
|
| 52 |
function testPHPFilter() {
|
| 53 |
// Log in as a user with permission to use the PHP code text format.
|
| 54 |
$php_code_permission = filter_permission_name(filter_format_load($this->php_code_format));
|
| 55 |
$web_user = $this->drupalCreateUser(array('access content', 'create page content', 'edit own page content', $php_code_permission));
|
| 56 |
$this->drupalLogin($web_user);
|
| 57 |
|
| 58 |
// Create a node with PHP code in it.
|
| 59 |
$node = $this->createNodeWithCode();
|
| 60 |
|
| 61 |
// Make sure that the PHP code shows up as text.
|
| 62 |
$this->drupalGet('node/' . $node->nid);
|
| 63 |
$this->assertText('print', t('PHP code is displayed.'));
|
| 64 |
|
| 65 |
// Change filter to PHP filter and see that PHP code is evaluated.
|
| 66 |
$edit = array();
|
| 67 |
$langcode = FIELD_LANGUAGE_NONE;
|
| 68 |
$edit["body[$langcode][0][value_format]"] = $this->php_code_format;
|
| 69 |
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
| 70 |
$this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title[FIELD_LANGUAGE_NONE][0]['value'])), t('PHP code filter turned on.'));
|
| 71 |
|
| 72 |
// Make sure that the PHP code shows up as text.
|
| 73 |
$this->assertNoText('print', t('PHP code isn\'t displayed.'));
|
| 74 |
$this->assertText('SimpleTest PHP was executed!', t('PHP code has been evaluated.'));
|
| 75 |
}
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Tests to make sure access to the PHP filter is properly restricted.
|
| 80 |
*/
|
| 81 |
class PHPAccessTestCase extends PHPTestCase {
|
| 82 |
public static function getInfo() {
|
| 83 |
return array(
|
| 84 |
'name' => 'PHP filter access check',
|
| 85 |
'description' => 'Make sure that users who don\'t have access to the PHP filter can\'t see it.',
|
| 86 |
'group' => 'PHP',
|
| 87 |
);
|
| 88 |
}
|
| 89 |
|
| 90 |
/**
|
| 91 |
* Make sure that user can't use the PHP filter when not given access.
|
| 92 |
*/
|
| 93 |
function testNoPrivileges() {
|
| 94 |
// Create node with PHP filter enabled.
|
| 95 |
$web_user = $this->drupalCreateUser(array('access content', 'create page content', 'edit own page content'));
|
| 96 |
$this->drupalLogin($web_user);
|
| 97 |
$node = $this->createNodeWithCode();
|
| 98 |
|
| 99 |
// Make sure that the PHP code shows up as text.
|
| 100 |
$this->drupalGet('node/' . $node->nid);
|
| 101 |
$this->assertText('print', t('PHP code is displayed.'));
|
| 102 |
|
| 103 |
// Make sure that user doesn't have access to filter.
|
| 104 |
$this->drupalGet('node/' . $node->nid . '/edit');
|
| 105 |
$this->assertNoRaw('<option value="' . $this->php_code_format . '">', t('PHP code format not available.'));
|
| 106 |
}
|
| 107 |
}
|