You are here

public function OptimizelyPageSnippetTest::testPageSnippetPresence in Optimizely 8

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

Test that snippet is present after project is enabled.

1. node/x, 2. article/one 3. node/x, article/one, node/x 4. article/one, node/x, article/two.

File

src/Tests/OptimizelyPageSnippetTest.php, line 228

Class

OptimizelyPageSnippetTest
Tests that the javascript snippet is included on a variety of paths.

Namespace

Drupal\optimizely\Tests

Code

public function testPageSnippetPresence() {
  $this
    ->drupalLogin($this->privilegedUser);

  // Enable all four projects.
  $edit = [
    'optimizely_enabled' => 1,
  ];
  $this
    ->drupalPostForm($this->update2Page, $edit, t('Update'));
  $this
    ->drupalPostForm($this->update3Page, $edit, t('Update'));
  $this
    ->drupalPostForm($this->update4Page, $edit, t('Update'));
  $this
    ->drupalPostForm($this->update5Page, $edit, t('Update'));

  // Test that Project 2 was enabled.
  $this
    ->drupalGet($this->listingPage);
  $this
    ->assertRaw('name="project-2" checked="checked"', '<strong>Project 2 is enabled, ready to test path settings for presence of snippet.</strong>', 'Optimizely');
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->anonymousUser);
  $num_projects = count($this->projectCode);
  for ($project_count = 0; $project_count <= $num_projects; $project_count++) {

    // Test project paths for presence of snippet.
    if (!empty($this->projectPaths[$project_count])) {
      $paths = explode("\n", $this->projectPaths[$project_count]);
    }
    else {
      $paths = [];
    }
    foreach ($paths as $path) {

      // End test if path value is invalid.
      if ($path == '') {
        break;
      }

      // Only test non wildcard paths.
      if (strpos($path, '/*') === FALSE) {

        // Look up the page defined in the project.
        $this
          ->drupalGet($path);
        $target_html = '"//cdn.optimizely.com/js/' . $this->projectCode[$project_count] . '.js"';
        $this
          ->assertRaw("src={$target_html}", "<strong>Optimizely snippet call {$target_html} found</strong> at: " . $path, 'Optimizely');
      }
    }
  }
}