3 namespace Drupal\php\Tests;
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\user\RoleInterface;
9 * Test if PHP filter works in general.
13 abstract class PhpTestBase extends WebTestBase {
20 public static $modules = ['node', 'php'];
22 protected $phpCodeFormat;
27 protected function setUp() {
30 // Create Basic page node type.
31 $this->drupalCreateContentType(['type' => 'page', 'name' => t('Basic page')]);
33 // Create and login admin user.
34 $admin_user = $this->drupalCreateUser(['administer filters']);
35 $this->drupalLogin($admin_user);
37 // Verify that the PHP code text format was inserted.
38 $php_format_id = 'php_code';
39 $this->phpCodeFormat = \Drupal::entityTypeManager()->getStorage('filter_format')->load($php_format_id);
41 $this->assertEqual($this->phpCodeFormat->label(), 'PHP code', 'PHP code text format was created.');
43 // Verify that the format has the PHP code filter enabled.
44 $filters = $this->phpCodeFormat->filters();
45 $this->assertTrue($filters->get('php_code')->status, 'PHP code filter is enabled.');
47 // Verify that the format exists on the administration page.
48 $this->drupalGet('admin/config/content/formats');
49 $this->assertText('PHP code', 'PHP code text format was created.');
51 // Verify that anonymous and authenticated user roles do not have access.
52 $this->drupalGet('admin/config/content/formats/manage/' . $php_format_id);
53 $this->assertFieldByName('roles[' . RoleInterface::ANONYMOUS_ID . ']', FALSE, 'Anonymous users do not have access to PHP code format.');
54 $this->assertFieldByName('roles[' . RoleInterface::AUTHENTICATED_ID . ']', FALSE, 'Authenticated users do not have access to PHP code format.');
58 * Creates a test node with PHP code in the body.
60 * @return \Drupal\node\NodeInterface
63 public function createNodeWithCode() {
64 return $this->drupalCreateNode(['body' => [['value' => '<?php print "SimpleTest PHP was executed!";print "Current state is " . Drupal::state()->get("php_state_test", "empty"); ?>']]]);