You are here

public function OptimizelyIncludeSnippetTest::testIncludeSnippet in Optimizely 8.3

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

Test whether snippet is correctly included or not included.

File

src/Tests/OptimizelyIncludeSnippetTest.php, line 63

Class

OptimizelyIncludeSnippetTest
Test that the javascript snippet is included on project pages.

Namespace

Drupal\optimizely\Tests

Code

public function testIncludeSnippet() {
  $this
    ->drupalLogin($this->privilegedUser);
  $node1 = $this
    ->makePage();
  $node2 = $this
    ->makePage();
  $node3 = $this
    ->makePage();
  $node4 = $this
    ->makePage();

  // Array holding project field values.
  $edit = [
    'optimizely_project_title' => $this
      ->randomMachineName(8),
    'optimizely_project_code' => rand(0, 10000),
    'optimizely_path' => "/node/" . $node1
      ->id() . "\n" . "/node/" . $node2
      ->id(),
    '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
  $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');
}