php.test 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @file
  4. * Tests for php.module.
  5. */
  6. /**
  7. * Defines a base PHP test case class.
  8. */
  9. class PHPTestCase extends DrupalWebTestCase {
  10. protected $php_code_format;
  11. function setUp() {
  12. parent::setUp('php');
  13. // Create and login admin user.
  14. $admin_user = $this->drupalCreateUser(array('administer filters'));
  15. $this->drupalLogin($admin_user);
  16. // Verify that the PHP code text format was inserted.
  17. $php_format_id = 'php_code';
  18. $this->php_code_format = filter_format_load($php_format_id);
  19. $this->assertEqual($this->php_code_format->name, 'PHP code', t('PHP code text format was created.'));
  20. // Verify that the format has the PHP code filter enabled.
  21. $filters = filter_list_format($php_format_id);
  22. $this->assertTrue($filters['php_code']->status, t('PHP code filter is enabled.'));
  23. // Verify that the format exists on the administration page.
  24. $this->drupalGet('admin/config/content/formats');
  25. $this->assertText('PHP code', t('PHP code text format was created.'));
  26. // Verify that anonymous and authenticated user roles do not have access.
  27. $this->drupalGet('admin/config/content/formats/' . $php_format_id);
  28. $this->assertFieldByName('roles[' . DRUPAL_ANONYMOUS_RID . ']', FALSE, t('Anonymous users do not have access to PHP code format.'));
  29. $this->assertFieldByName('roles[' . DRUPAL_AUTHENTICATED_RID . ']', FALSE, t('Authenticated users do not have access to PHP code format.'));
  30. }
  31. /**
  32. * Creates a test node with PHP code in the body.
  33. *
  34. * @return stdObject Node object.
  35. */
  36. function createNodeWithCode() {
  37. return $this->drupalCreateNode(array('body' => array(LANGUAGE_NONE => array(array('value' => '<?php print "SimpleTest PHP was executed!"; ?>')))));
  38. }
  39. }
  40. /**
  41. * Tests to make sure the PHP filter actually evaluates PHP code when used.
  42. */
  43. class PHPFilterTestCase extends PHPTestCase {
  44. public static function getInfo() {
  45. return array(
  46. 'name' => 'PHP filter functionality',
  47. 'description' => 'Make sure that PHP filter properly evaluates PHP code when enabled.',
  48. 'group' => 'PHP',
  49. );
  50. }
  51. /**
  52. * Makes sure that the PHP filter evaluates PHP code when used.
  53. */
  54. function testPHPFilter() {
  55. // Log in as a user with permission to use the PHP code text format.
  56. $php_code_permission = filter_permission_name(filter_format_load('php_code'));
  57. $web_user = $this->drupalCreateUser(array('access content', 'create page content', 'edit own page content', $php_code_permission));
  58. $this->drupalLogin($web_user);
  59. // Create a node with PHP code in it.
  60. $node = $this->createNodeWithCode();
  61. // Make sure that the PHP code shows up as text.
  62. $this->drupalGet('node/' . $node->nid);
  63. $this->assertText('print "SimpleTest PHP was executed!"', t('PHP code is displayed.'));
  64. // Change filter to PHP filter and see that PHP code is evaluated.
  65. $edit = array();
  66. $langcode = LANGUAGE_NONE;
  67. $edit["body[$langcode][0][format]"] = $this->php_code_format->format;
  68. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  69. $this->assertRaw(t('Basic page %title has been updated.', array('%title' => $node->title)), t('PHP code filter turned on.'));
  70. // Make sure that the PHP code shows up as text.
  71. $this->assertNoText('print "SimpleTest PHP was executed!"', t("PHP code isn't displayed."));
  72. $this->assertText('SimpleTest PHP was executed!', t('PHP code has been evaluated.'));
  73. }
  74. }
  75. /**
  76. * Tests to make sure access to the PHP filter is properly restricted.
  77. */
  78. class PHPAccessTestCase extends PHPTestCase {
  79. public static function getInfo() {
  80. return array(
  81. 'name' => 'PHP filter access check',
  82. 'description' => 'Make sure that users who don\'t have access to the PHP filter can\'t see it.',
  83. 'group' => 'PHP',
  84. );
  85. }
  86. /**
  87. * Makes sure that the user can't use the PHP filter when not given access.
  88. */
  89. function testNoPrivileges() {
  90. // Create node with PHP filter enabled.
  91. $web_user = $this->drupalCreateUser(array('access content', 'create page content', 'edit own page content'));
  92. $this->drupalLogin($web_user);
  93. $node = $this->createNodeWithCode();
  94. // Make sure that the PHP code shows up as text.
  95. $this->drupalGet('node/' . $node->nid);
  96. $this->assertText('print', t('PHP code was not evaluated.'));
  97. // Make sure that user doesn't have access to filter.
  98. $this->drupalGet('node/' . $node->nid . '/edit');
  99. $this->assertNoRaw('<option value="' . $this->php_code_format->format . '">', t('PHP code format not available.'));
  100. }
  101. }