You are here

public function OptimizelyAliasIncludeSnippetTest::testIncludeSnippet in Optimizely 8

Same name and namespace in other branches
  1. 8.3 src/Tests/OptimizelyAliasIncludeSnippetTest.php \Drupal\optimizely\Tests\OptimizelyAliasIncludeSnippetTest::testIncludeSnippet()
  2. 8.0 src/Tests/OptimizelyAliasIncludeSnippetTest.php \Drupal\optimizely\Tests\OptimizelyAliasIncludeSnippetTest::testIncludeSnippet()

Test inclusion and non-inclusion of snippet.

File

src/Tests/OptimizelyAliasIncludeSnippetTest.php, line 66

Class

OptimizelyAliasIncludeSnippetTest
Tests pages that have aliases.

Namespace

Drupal\optimizely\Tests

Code

public function testIncludeSnippet() {
  $this
    ->drupalLogin($this->privilegedUser);
  $node1 = $this
    ->makePage();
  $node2 = $this
    ->makePage();
  $node3 = $this
    ->makePage();
  $node4 = $this
    ->makePage();
  $alias1 = $this
    ->makePageAlias($node1);
  $alias2 = $this
    ->makePageAlias($node2);
  $alias3 = $this
    ->makePageAlias($node3);
  $alias4 = $this
    ->makePageAlias($node4);

  // Array holding project field values.
  $edit = [
    'optimizely_project_title' => $this
      ->randomMachineName(8),
    'optimizely_project_code' => rand(0, 10000),
    'optimizely_path' => $alias1 . "\n" . $alias2,
    'optimizely_enabled' => 1,
  ];

  // Create snippet.
  $snippet = '//cdn.optimizely.com/js/' . $edit['optimizely_project_code'] . '.js';

  // Create the project.
  $this
    ->drupalPostForm($this->addUpdatePage, $edit, t('Add'));

  // Log out to make sure cache refreshing works.
  $this
    ->drupalLogout();

  // @todo check how to turn on "cache pages for anonymous users"
  // and "Aggregate JavaScript files" on Performance page
  // Check if snippet does appears on project path pages thru alias
  $this
    ->drupalGet("node/" . $node1
    ->id());
  $this
    ->assertRaw($snippet, '<strong>Snippet found in markup of project path page</strong>', 'Optimizely');
  $this
    ->drupalGet("node/" . $node2
    ->id());
  $this
    ->assertRaw($snippet, '<strong>Snippet found in markup of project path page</strong>', 'Optimizely');

  // Check if snippet does not appear on other non-project pages.
  $this
    ->drupalGet("node/" . $node3
    ->id());
  $this
    ->assertNoRaw($snippet, '<strong>Snippet not found in markup of other page</strong>', 'Optimizely');
  $this
    ->drupalGet("node/" . $node4
    ->id());
  $this
    ->assertNoRaw($snippet, '<strong>Snippet not found in markup of other page</strong>', 'Optimizely');
}