You are here

public function OptimizelyPageSnippetTest::setUp in Optimizely 8.0

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

Sets up a Drupal site for running functional and integration tests.

Installs Drupal with the installation profile specified in \Drupal\simpletest\WebTestBase::$profile into the prefixed database.

Afterwards, installs any additional modules specified in the static \Drupal\simpletest\WebTestBase::$modules property of each class in the class hierarchy.

After installation all caches are flushed and several configuration values are reset to the values of the parent site executing the test, since the default values may be incompatible with the environment in which tests are being executed.

Overrides WebTestBase::setUp

File

src/Tests/OptimizelyPageSnippetTest.php, line 63

Class

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

Namespace

Drupal\optimizely\Tests

Code

public function setUp() {
  parent::setUp();
  $this
    ->drupalCreateContentType(array(
    'type' => 'page',
    'name' => 'Basic page',
  ));
  $this->anonymousUser = $this
    ->drupalCreateUser(array(
    'access content',
  ));
  $this->privilegedUser = $this
    ->drupalCreateUser(array(
    'access content',
    'create page content',
    'administer url aliases',
    'create url aliases',
    $this->optimizelyPermission,
  ));

  /*
   * Pages
   * 1. 1 x page (node/x), no alias
   * 2. 1 x page, article
   * 2. 3 x page (node/x), 2 x alias - "article/one, article/two"
   * 3. 2 x sub page (node/x), 2 x alias - "article/one/sub, article/two/sub"
   * 4. <front>, node/x, article/three
   *
   * Projects
   * 1. node/x,
   * 2. article/one
   * 3. node/x, article/one, node/x
   * 4. article/one, node/x, article/two
   * 5. node/*
   * 6. article/* <-- Multi matches: article, article/one, article/two, article/one/sub, article/two/sub
   * 7. <front> (article/one)
   * 8. <front>, article/one <-- non unique path
   * 9. node/*, article/* <-- non unique path
   * 10. article/one/* <-- Multi matches: article/one/sub
   * 11. article, article/one, article/one/*
   * 12. article, node/x, article/one, article/two/*
   * 13. node/x, article/one, article/two/*, user, user/*
   * 14. article/one?param=xx&parm2=xxx
   * 15. node/x, article/one, article/two/*, user/*, article?param=xx&parm2=xxx
   *
   * ++ multi projects enabled
   */

  // Test page creation at base alias path
  $this->projectNodes = array();
  $this->projectAliases = array();
  $this->projectPaths = array();
  $subpath = '';

  // Access with privileged user
  $this
    ->drupalLogin($this->privilegedUser);
  for ($project_count = 0; $project_count < 4; $project_count++) {
    $this->projectPaths[$project_count] = '';
    for ($page_count = 0; $page_count < 5; $page_count++) {

      // create page
      $settings = array(
        'type' => 'page',
        'title' => $this
          ->randomMachineName(32),
        'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
        'body' => array(
          array(
            'value' => $this
              ->randomMachineName(64),
            'format' => filter_default_format(),
          ),
        ),
      );
      $node = $this
        ->drupalCreateNode($settings);

      // Keep track of the nids created
      $this->projectNodes[] = $node
        ->id();
      $random_alias_node = mt_rand(0, 1);
      if ($random_alias_node) {
        $alias = '/' . $this
          ->randomMachineName(10);

        // Random subpath alias
        $random_subalias_node = mt_rand(0, 1);
        if ($random_subalias_node) {
          $subpath = '/' . $this
            ->randomMachineName(10);
          $alias = $subpath . $alias;
        }

        // Create the url alias
        $edit_node = array();
        $edit_node['source'] = '/node/' . $node
          ->id();
        $edit_node['alias'] = $alias;
        $this
          ->drupalPostForm($this->addAliasPage, $edit_node, t('Save'));

        // Keep track of alias created
        $this->projectAliases[] = $alias;

        // Randomly create wildcard project path entry
        $random_project_wildcard = mt_rand(0, 1);
        if ($random_project_wildcard && $subpath != '') {
          $edit_node['alias'] = $subpath . '/*';
          $subpath = '';

          // Add alias to project path setting variable
          if (!empty($this->projectPaths[$project_count])) {
            $this->projectPaths[$project_count] = $edit_node['alias'] . "\n" . $this->projectPaths[$project_count];
          }
          else {
            $this->projectPaths[$project_count] = $edit_node['alias'];
          }
        }
        elseif ($subpath == '') {
          if (!empty($this->projectPaths[$project_count])) {
            $this->projectPaths[$project_count] = $edit_node['alias'] . "\n" . $this->projectPaths[$project_count];
          }
          else {
            $this->projectPaths[$project_count] = $edit_node['alias'];
          }
        }
      }
      else {
        if (!empty($this->projectPaths[$project_count])) {
          $this->projectPaths[$project_count] = '/node/' . $node
            ->id() . "\n" . $this->projectPaths[$project_count];
        }
        else {
          $this->projectPaths[$project_count] = '/node/' . $node
            ->id();
        }
      }
    }

    // Create Projects
    $this->projectCode[$project_count] = mt_rand(0, 10000);

    // Add project with path setting to page
    $edit = array(
      'optimizely_project_title' => $this
        ->randomMachineName(8),
      'optimizely_project_code' => $this->projectCode[$project_count],
      'optimizely_path' => $this->projectPaths[$project_count],
      'optimizely_enabled' => 0,
    );
    $this
      ->drupalPostForm($this->addUpdatePage, $edit, t('Add'));
  }
}