3 namespace Drupal\php\Tests;
6 * Tests to make sure the PHP filter actually evaluates PHP code when used.
10 class PhpFilterTest extends PhpTestBase {
13 * Makes sure that the PHP filter evaluates PHP code when used.
15 public function testPhpFilter() {
16 // Log in as a user with permission to use the PHP code text format.
17 $php_code_permission = entity_load('filter_format', 'php_code')->getPermissionName();
20 'create page content',
21 'edit own page content',
24 $web_user = $this->drupalCreateUser($permissions);
25 $this->drupalLogin($web_user);
27 // Create a node with PHP code in it.
28 $node = $this->createNodeWithCode();
30 // Make sure that the PHP code shows up as text.
31 $this->drupalGet('node/' . $node->id());
32 $this->assertText('php print');
34 // Change filter to PHP filter and see that PHP code is evaluated.
36 $edit['body[0][format]'] = $this->phpCodeFormat->id();
37 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
38 $this->assertRaw(t('@type %title has been updated.', ['@type' => 'Basic page', '%title' => $node->link($node->getTitle())]), 'PHP code filter turned on.');
40 // Make sure that the PHP code shows up as text.
41 $this->assertNoText('print "SimpleTest PHP was executed!"', "PHP code isn't displayed.");
42 $this->assertText('SimpleTest PHP was executed!', 'PHP code has been evaluated.');
44 // Verify that cache is disabled for PHP evaluates.
45 $this->assertText('Current state is empty', 'PHP code has been evaluated once.');
46 \Drupal::state()->set('php_state_test', 'not empty');
47 $this->drupalGet('node/' . $node->id());
48 $this->assertText('Current state is not empty', 'PHP code has been evaluated again.');