5 * Definition of Drupal\php\Tests\PhpTestBase.
8 namespace Drupal\php\Tests;
10 use Drupal\simpletest\WebTestBase;
11 use Drupal\user\RoleInterface;
14 * Test if PHP filter works in general.
18 abstract class PhpTestBase extends WebTestBase {
25 public static $modules = ['node', 'php'];
27 protected $phpCodeFormat;
32 protected function setUp() {
35 // Create Basic page node type.
36 $this->drupalCreateContentType(['type' => 'page', 'name' => t('Basic page')]);
38 // Create and login admin user.
39 $admin_user = $this->drupalCreateUser(['administer filters']);
40 $this->drupalLogin($admin_user);
42 // Verify that the PHP code text format was inserted.
43 $php_format_id = 'php_code';
44 $this->phpCodeFormat = \Drupal::entityTypeManager()->getStorage('filter_format')->load($php_format_id);
46 $this->assertEqual($this->phpCodeFormat->label(), 'PHP code', 'PHP code text format was created.');
48 // Verify that the format has the PHP code filter enabled.
49 $filters = $this->phpCodeFormat->filters();
50 $this->assertTrue($filters->get('php_code')->status, 'PHP code filter is enabled.');
52 // Verify that the format exists on the administration page.
53 $this->drupalGet('admin/config/content/formats');
54 $this->assertText('PHP code', 'PHP code text format was created.');
56 // Verify that anonymous and authenticated user roles do not have access.
57 $this->drupalGet('admin/config/content/formats/manage/' . $php_format_id);
58 $this->assertFieldByName('roles[' . RoleInterface::ANONYMOUS_ID . ']', FALSE, 'Anonymous users do not have access to PHP code format.');
59 $this->assertFieldByName('roles[' . RoleInterface::AUTHENTICATED_ID . ']', FALSE, 'Authenticated users do not have access to PHP code format.');
63 * Creates a test node with PHP code in the body.
65 * @return \Drupal\node\NodeInterface
68 public function createNodeWithCode() {
69 return $this->drupalCreateNode(['body' => [['value' => '<?php print "SimpleTest PHP was executed!"; ?>']]]);