You are here

public function OptimizelyAliasIncludeSnippetTest::testIncludeSnippet in Optimizely 8.0

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

File

src/Tests/OptimizelyAliasIncludeSnippetTest.php, line 65
Contains \Drupal\optimizely\Tests\OptimizelyAliasIncludeSnippetTest

Class

OptimizelyAliasIncludeSnippetTest
Tests that the javascript snippet is included on project pages through aliases and not included on non-project pages through 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 = array(
    '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');
}