You are here

optimizely.test in Optimizely 7.2

Same filename and directory in other branches
  1. 7.3 optimizely.test

Optimizely Tests

File

optimizely.test
View source
<?php

/**
 * @file
 * Optimizely Tests
 */

/**
 * Web Tests
 * 
 * OptimizelyTestModuleSetupCase: Check default database sets up correctly.
 */

/**
 * OptimizelyTestUserRoleTestCase: Create anonymous, authenticated and privileged user to test access to module related pages.
 */
class OptimizelyTestUserRoleTestCase extends DrupalWebTestCase {

  //Public, Private, Protected: http://stackoverflow.com/questions/4361553/php-public-private-protected
  protected $anonymous_user;
  protected $authenticated_user;
  protected $privileged_user;

  /**
   * OptimizelyTestAdminRoleCase getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Access Test',
      'description' => 'Test that no part of the Optimizely module administration interface can be accessed without the necessary permissions.',
      'group' => 'Optimizely',
    );
  }

  /**
   * OptimizelyTestAdminRoleCase setUp().
   */
  public function setUp() {

    // Enable any modules required for the test
    parent::setUp('optimizely');
    $this->anonymous_user = $this
      ->drupalCreateUser(array());
    $this->authenticated_user = $this
      ->drupalCreateUser(array(
      'access content',
    ));

    // Create and log in an admin user. The user will have the privilege
    // 'administer optimizely'. This privaged is need to access all administration
    // functionality of the module.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'administer optimizely',
      'create page content',
      'edit own page content',
      'administer url aliases',
      'create url aliases',
    ));
  }

  /**
   * OptimizelyTestAdminRoleCase testOptimizelyTestUserRolePublicAccess()
   */
  public function testOptimizelyTestUserRolePublicAccess() {
    for ($i = 1; $i <= 2; $i++) {
      if ($i == 1) {
        $target = $i . '. <strong>Anonymous</strong>';
        $this
          ->drupalLogin($this->anonymous_user);
      }
      else {
        $target = $i . '. <strong>Authenticated</strong>';
        $this
          ->drupalLogin($this->authenticated_user);
      }

      // Tests
      $this
        ->drupalGet('admin/config/system/optimizely');
      $this
        ->assertNoRaw('<h1 class="page-title">Optimizely</h1>', $target . ' user *<strong>can not</strong>* access project listing page -> admin/config/system/optimizely');
      $this
        ->drupalGet('admin/config/system/optimizely/default');
      $this
        ->assertNoRaw('<h1 class="page-title">Optimizely</h1>', $target . ' user *<strong>can not</strong>* access project listing page -> admin/config/system/optimizely/default');
      $this
        ->drupalGet('admin/config/system/optimizely/add_update');
      $this
        ->assertNoRaw('<h1 class="page-title">Optimizely</h1>', $target . ' user *<strong>can not</strong>* access project add form page -> admin/config/system/optimizely/add_update');
      $this
        ->drupalGet('admin/config/system/optimizely/settings');
      $this
        ->assertNoRaw('<h1 class="page-title">Optimizely</h1>', $target . ' user *<strong>can not</strong>* access settings page -> admin/config/system/optimizely/settings');
      $this
        ->drupalGet('admin/config/system/optimizely/ajax');
      $this
        ->assertNoRaw('<h1 class="page-title">Optimizely</h1>', $target . ' user *<strong>can not</strong>* access AJAX callback URL -> /admin/config/system/optimizely/ajax');
      $this
        ->drupalLogout();
    }
  }

  /**
   * OptimizelyTestAdminRoleCase testOptimizelyTestUserRoleAdminAccess()
   */
  public function testOptimizelyTestUserRoleAdminAccess() {
    $this
      ->drupalLogin($this->privileged_user);
    $this
      ->drupalGet('admin/config/system/optimizely');
    $this
      ->assertNoRaw('Access denied', '** <strong>Admin user can access</strong> project listing page -> admin/config/system/optimizely');
    $this
      ->drupalGet('admin/config/system/optimizely/default');
    $this
      ->assertNoRaw('Access denied', '** <strong>Admin user can access</strong> project listing page -> admin/config/system/optimizely/default');
    $this
      ->drupalGet('admin/config/system/optimizely/add_update');
    $this
      ->assertNoRaw('Access denied', '** <strong>Admin user can access</strong> add project form page -> admin/config/system/optimizely/add_update');
    $this
      ->drupalGet('admin/config/system/optimizely/settings');
    $this
      ->assertNoRaw('Access denied', '** <strong>Admin user can access</strong> settings page -> admin/config/system/optimizely/settings');
    $this
      ->drupalGet('admin/config/system/optimizely/ajax');
    $this
      ->assertNoRaw('Access denied', 'Admin user can access AJAX callback URL -> admin/config/system/optimizely/ajax');

    // admin/config/system/optimizely/add_update/%
    // admin/config/system/optimizely/delete/%
    $this
      ->drupalLogout();
  }

}

/**
 * OptimizelyTestDefaultProjectTestCase: Test that:
 *
 * 1. The default project is available but disabled in the project listing page after the module has been enabled.
 * 2. A message in the project listing page directs the administrator to go to the module settings page to enter the Optimizely account value.
 * 3. Accessing the account setting page should be blank by default with a message informing the user that the account setting will be used
 *    for the default project number.
 * 4. Test adding the account setting redirects to the project listing page with the account number listed as the disabled project dumber for the
 *    default project entry.
 * 5. The default project can not be enabled until the account number is entered on the settings page.
 * 6. Enabling the default project with the default path setting of side wide "*" should result in the snippet being displayed on the sites front page.
 */
class OptimizelyTestDefaultProjectTestCase extends DrupalWebTestCase {
  protected $settingsPage = 'admin/config/system/optimizely/settings';
  protected $listingPage = 'admin/config/system/optimizely';
  protected $updateDefaultProjPage = 'admin/config/system/optimizely/add_update/1';
  protected $ajaxCallbackUrl = 'admin/config/system/optimizely/ajax';
  protected $optimizelyPermission = 'administer optimizely';
  protected $anonymous_user;
  protected $authenticated_user;
  protected $privileged_user;
  protected $optimizely_account_id;

  // Modules to enable.
  public static $modules = array(
    'optimizely',
  );

  /**
   * OptimizelyTestDefaultProjectTestCase getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Default Project Test',
      'description' => 'Test the exsistence of a disabled default project entry that when enabled after adding the Optimizely account ID results in the default snippeting being added to the front page (default) of the site.',
      'group' => 'Optimizely',
    );
  }

  /**
   * OptimizelyTestDefaultProjectTestCase setUp().
   */
  public function setUp() {

    // Enable any modules required for the test
    parent::setUp('optimizely');
    $this->anonymous_user = $this
      ->drupalCreateUser(array());
    $this->authenticated_user = $this
      ->drupalCreateUser(array(
      'access content',
    ));

    // Create and log in an admin user. The user will have the privilege
    // 'administer optimizely'. These privileges are needed to access all administration
    // functionality of the module.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'administer optimizely',
      'create page content',
      'edit own page content',
      'administer url aliases',
      'create url aliases',
    ));
  }

  /*
   * 1. The default project is available but disabled in the project listing page
   *    after the module has been enabled.
   * 2. A message in the project listing page directs the administrator to go to
   *    the module settings page to enter the Optimizely account value.
   * 5. The default project can not be enabled until the account number is entered on the settings page.
   */
  public function testOptimizelyTestDefaultProjectEnable() {

    // Access with privileged user
    $this
      ->drupalLogin($this->privileged_user);

    // Look for entry in project listing page
    $this
      ->drupalGet('admin/config/system/optimizely');
    $this
      ->assertRaw('<td class="project-title-column disabled">Default</td>', '** <strong>Default project entry fround on project listing page.</strong>');

    // Confirm default project is not enabled
    $this
      ->assertRaw('<input id="project-enable-1" name="project-1" type="checkbox" value="1" class="form-checkbox" />', '** <strong>Default project is not enabled.</strong>');

    // Link to complete default project setup available
    $this
      ->assertRaw('<strong><a href="/admin/config/system/optimizely/settings">Account Info</a></strong>', '** <strong>Link from default project to module settings page available.</strong>');

    // Navigate to Edit form for Default project
    $this
      ->drupalGet('admin/config/system/optimizely/add_update/1');

    // Title field set to Default, not accessable
    $this
      ->assertRaw('<input disabled="disabled" type="text" id="edit-optimizely-project-title" name="optimizely_project_title" value="Default"', '** <strong>Project title field is not editable and set to "Default"</strong>.');

    // Project Code field not set (Undefined), not accessable
    $this
      ->assertRaw('<input disabled="disabled" type="text" id="edit-optimizely-project-code" name="optimizely_project_code" value="Undefined"', '** <strong>Project code field is not editable and set to "Undefined".</strong>');

    // Link to settings page to set account / Default project code
    $this
      ->assertRaw('<a href="/admin/config/system/optimizely/settings">', '** <strong>Link to settings page found to set Default project code.</strong>');

    // Check default Default project path is set to site wide wild card
    $this
      ->assertRaw('name="optimizely_path" cols="100" rows="6" class="form-textarea">*</textarea>', '** <strong>Default project path set to site wide wild card "*".</strong>');

    // * 5. The default project can not be enabled until the account number is entered on the settings page.
    $this
      ->drupalLogout();
  }

  /*
   * 3. Accessing the account setting page should be blank by default with a message informing the user that the account setting will be used
   *    for the default project number.
   * 4. Test adding the account setting redirects to the project listing page with the account number listed as the disabled project number for the
   *    default project entry.
   */
  public function testOptimizelyTestDefaultProjectSettings() {

    // Access with privileged user
    $this
      ->drupalLogin($this->privileged_user);

    // Access general module settings page
    $this
      ->drupalGet('admin/config/system/optimizely/settings');

    // Check for blank setting (default)
    $this
      ->assertFieldByName('optimizely_id', NULL, '** <strong>The Optimizely ID field is blank</strong> on Settings page: admin/config/system/optimizely/settings');

    // Add Optimizely account setting
    $this->optimizely_account_id = rand(1000000, 9999999);
    $edit = array(
      'optimizely_id' => $this->optimizely_account_id,
    );
    $this
      ->drupalPost('admin/config/system/optimizely/settings', $edit, t('Submit'));
    $this
      ->drupalGet('/admin/config/system/optimizely');

    // Check that the newly entered Optimizely ID is now listed as the project ID for the Default project
    $this
      ->assertRaw('<td class="project-code-column disabled">' . $this->optimizely_account_id . '</td>', '** <strong>Default project is using the Optimizely account setting for project ID -> ' . $this->optimizely_account_id . '.</strong>');

    // Access add / edit project page for default project
    $this
      ->drupalGet('/admin/config/system/optimizely/add_update/1');

    // Check the project ID setting matches the Optimizely Account ID setting.
    $this
      ->assertFieldByName('optimizely_project_code', $this->optimizely_account_id, '** <strong>The Optimizely Project Code matches the Optimizely account ID setting.</strong>');

    // Enable the Default project
    $edit = array(
      'optimizely_enabled' => 1,
    );
    $this
      ->drupalPost('/admin/config/system/optimizely/add_update/1', $edit, t('Update'));

    // Go to project listings page
    $this
      ->drupalGet('admin/config/system/optimizely');

    // Confirm default project *is* enabled
    $this
      ->assertRaw('<input id="project-enable-1" name="project-1" checked="checked" type="checkbox" value="1" class="form-checkbox" />', '** <strong>Default project *is* enabled on project listing page.</strong>');
    $this
      ->drupalLogout();
  }

  /*
   * Test the AJAX functionality of disabling/enabling the default project.
   */
  public function testOptimizelyTestDefaultProjectListingAJAX() {

    // Access with privileged user
    $this
      ->drupalLogin($this->privileged_user);

    // Access general module settings page
    $this
      ->drupalGet('admin/config/system/optimizely/settings');

    // Add Optimizely account setting so that Default Project can be enabled.
    $this->optimizelyAccountId = rand(1000000, 9999999);
    $edit = array(
      'optimizely_id' => $this->optimizelyAccountId,
    );
    $this
      ->drupalPost($this->settingsPage, $edit, t('Submit'));

    // Confirm default project is disabled.
    $this
      ->assertNoFieldChecked('project-enable-1', '<strong>Default project is disabled on project listing page.</strong>', 'Optimizely');
    $protocol = empty($_SERVER['HTTPS']) ? 'http' : 'https';
    $domain = $_SERVER['SERVER_NAME'];

    // Test that Ajax call succeeds.
    $params = array(
      'target_oid' => 1,
      // 1 == Default Project
      'target_enable' => 1,
    );
    $curlParams = array(
      CURLOPT_URL => $protocol . '://' . $domain . ':' . $_SERVER['SERVER_PORT'] . '/' . $this->ajaxCallbackUrl,
      CURLOPT_POST => TRUE,
      CURLOPT_POSTFIELDS => drupal_http_build_query($params),
      CURLOPT_HTTPHEADER => array(
        "Accept: application/json",
      ),
      CURLOPT_RETURNTRANSFER => TRUE,
      CURLOPT_VERBOSE => TRUE,
    );
    $ajaxResults = $this
      ->curlExec($curlParams, TRUE);
    $ajaxResponse = json_decode($ajaxResults);
    $this
      ->assertEqual($ajaxResponse->status, 'updated', '<strong>AJAX call to /admin/config/system/optimizely/ajax returned status is "updated"</strong>', 'Optimizely');
    $this
      ->assertEqual($ajaxResponse->message, '', '<strong>AJAX call to /admin/config/system/optimizely/ajax returned message as blank</strong>', 'Optimizely');

    // Go to project listings page
    $this
      ->drupalGet('admin/config/system/optimizely');

    // Confirm default project is enabled.
    $this
      ->assertFieldChecked('project-enable-1', '<strong>Default project is enabled on project listing page.</strong>', 'Optimizely');
  }

}

/*
 * Test for the presence of the Optimizely javascript snippet in various project
 * setting combinations. This includes combinations that use wildcard paths.
 */
class OptimizelyTestPageSnippetTestCase extends DrupalWebTestCase {
  protected $anonymous_user;
  protected $authenticated_user;
  protected $privileged_user;
  protected $project_code;
  protected $project_nodes;
  protected $project_paths;
  protected $project_aliases;

  /**
   * OptimizelyTestPageSnippetTestCase getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Presence of Optimizely Javascript Snippet Test',
      'description' => 'Test the presence of the Optimizely snippet (Javascript call) on pages (paths) defined in project entries.',
      'group' => 'Optimizely',
    );
  }

  /**
   * OptimizelyTestPageSnippetTestCase setUp().
   */
  public function setUp() {

    // Enable any modules required for the test
    parent::setUp('optimizely');
    $this->anonymous_user = $this
      ->drupalCreateUser(array());
    $this->authenticated_user = $this
      ->drupalCreateUser(array(
      'access content',
    ));

    // Create and log in an admin user. The user will have the privilege
    // 'administer optimizely'. This privaged is need to access all administration
    // functionality of the module.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'administer optimizely',
      'create page content',
      'edit own page content',
      'administer url aliases',
      'create url aliases',
    ));

    /*
     * 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->project_nodes = array();
    $this->project_aliases = array();
    $this->project_paths = array();
    $subpath = '';

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

        // create page
        $settings = array(
          'type' => 'page',
          'title' => $this
            ->randomName(32),
          'body' => array(
            LANGUAGE_NONE => array(
              array(
                $this
                  ->randomName(64),
              ),
            ),
          ),
        );
        $node = $this
          ->drupalCreateNode($settings);

        // Keep track of the nids created
        $this->project_nodes[] = $node->nid;
        $random_alias_node = rand(0, 1);
        if ($random_alias_node) {
          $alias = $this
            ->randomName(10);

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

          // Create the url alias
          $edit_node = array();
          $edit_node['source'] = 'node/' . $node->nid;
          $edit_node['alias'] = $alias;
          $alias_details = array(
            "source" => $edit_node['source'],
            "alias" => $edit_node['alias'],
          );
          path_save($alias_details);

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

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

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

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

      // Add project with path setting to page
      $edit = array(
        'optimizely_project_title' => $this
          ->randomName(8),
        'optimizely_project_code' => $this->project_code[$project_count],
        'optimizely_path' => $this->project_paths[$project_count],
        'optimizely_enabled' => 0,
      );
      $this
        ->drupalPost('admin/config/system/optimizely/add_update', $edit, t('Add'));
    }
  }

  /*
   * 1. node/x,
   * 2. article/one
   * 3. node/x, article/one, node/x
   * 4. article/one, node/x, article/two
   */
  public function testOptimizelyTestPageSnippetTestForPresence() {
    $project_count = 0;
    while ($project_count < count($this->project_paths)) {
      $target_project = $project_count + 2;

      // Access with privileged user
      $this
        ->drupalLogin($this->privileged_user);

      // Enable project 1
      $edit = array(
        'optimizely_enabled' => 1,
      );
      $this
        ->drupalPost('admin/config/system/optimizely/add_update/' . $target_project, $edit, t('Update'));

      // Test Project was enabled
      $this
        ->drupalGet('admin/config/system/optimizely');
      $this
        ->assertRaw('name="project-' . $target_project . '" checked="checked"', '** <strong>Project ' . $target_project . ' is enabled</strong>, ready to test path settings for presance of snippet.');
      $this
        ->drupalLogout();
      $this
        ->drupalLogin($this->anonymous_user);

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

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

          // Look up the page defained in the project
          $this
            ->drupalGet($path);

          // Confirm default project is *enabled*
          $this
            ->assertRaw('http://cdn.optimizely.com/js/' . $this->project_code[$project_count] . '.js', '** <strong>Optimizely snippet call http://cdn.optimizely.com/js/' . $this->project_code[$project_count] . '.js found</strong> at: ' . $path);
        }
      }
      $this
        ->drupalLogout();

      // Disable project
      // Access with privileged user
      $this
        ->drupalLogin($this->privileged_user);
      $edit = array(
        'optimizely_enabled' => 0,
      );
      $this
        ->drupalPost('admin/config/system/optimizely/add_update/' . $target_project, $edit, t('Update'));
      $this
        ->drupalLogout();
      $project_count++;
    }
  }

  /*
   * 5. node/*
   * 6. article/* <-- Multi matches: article, article/one, article/two, article/one/sub, article/two/sub
   */
  public function testOptimizelyTestPageSnippetTestForPresenceWildcard() {

    // Access with privileged user
    $this
      ->drupalLogin($this->anonymous_user);

    // @todo: Test Project is enabled
    for ($project_count = 0; $project_count <= 3; $project_count++) {
      $paths = explode("\n", $this->project_paths[$project_count]);
      foreach ($paths as $path) {

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

        // Wildcard found
        if (strpos($path, '/*') !== FALSE) {

          // @todo: Lookup all page paths that match wildcard
          // Go to paths that match wildcard project entry
          $this
            ->drupalGet($path);

          // Confirm Optimizely snippet is found at page that matches wildcard path
          // $this->assertRaw('<script type="text/javascript" src="http://cdn.optimizely.com/js/' . $this->project_code . '.js"></script>', '** <strong>Optimizely snippet call http://cdn.optimizely.com/js/' . $this->project_code . '.js found</strong> at: ' . $path . ' wildcard path.');
        }
      }
    }
  }

}
class OptimizelyTestAddUpdateCase extends DrupalWebTestCase {
  protected $privileged_user;

  /**
   * OptimizelyTestAddUpdateCase getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Add / Update Projects Test',
      'description' => 'Ensure that the add / update features function properly.',
      'group' => 'Optimizely',
    );
  }

  /**
   * OptimizelyTestAddUpdateCase setUp().
   */
  public function setUp() {
    parent::setUp('optimizely');

    // Enable any modules required for the test
    // Create and log in our user. The user has the arbitrary privilege
    // 'extra special edit any Optimizely project' which the code uses
    // to grant access.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'administer optimizely',
      'create page content',
      'edit own page content',
      'administer url aliases',
      'create url aliases',
    ));
    $this
      ->drupalLogin($this->privileged_user);
  }
  public function testOptimizelyAddUpdateProject() {

    // create page
    $settings = array(
      'type' => 'page',
      'title' => $this
        ->randomName(32),
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            $this
              ->randomName(64),
          ),
        ),
      ),
    );
    $node1 = $this
      ->drupalCreateNode($settings);

    // Create the url alias
    $edit_node1 = array();
    $edit_node1['source'] = 'node/' . $node1->nid;
    $edit_node1['alias'] = $this
      ->randomName(10);
    $this
      ->drupalPost('admin/config/search/path/add', $edit_node1, t('Save'));
    $edit = array(
      'optimizely_project_title' => $this
        ->randomName(8),
      'optimizely_project_code' => rand(0, 10000),
      'optimizely_path' => $edit_node1['alias'],
      'optimizely_enabled' => rand(0, 1),
    );
    $this
      ->drupalPost('admin/config/system/optimizely/add_update', $edit, t('Add'));
    $project_title = db_query('SELECT project_title FROM {optimizely} WHERE project_title = :optimizely_project_title', array(
      ':optimizely_project_title' => $edit['optimizely_project_title'],
    ))
      ->fetchField();
    $this
      ->assertEqual($project_title, $edit['optimizely_project_title'], t('The project was added to the database.'));

    // create page
    $settings = array(
      'type' => 'page',
      'title' => $this
        ->randomName(32),
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            $this
              ->randomName(64),
          ),
        ),
      ),
    );
    $node2 = $this
      ->drupalCreateNode($settings);

    // Create the url alias
    $edit_node2 = array();
    $edit_node2['source'] = 'node/' . $node2->nid;
    $edit_node2['alias'] = $this
      ->randomName(10);
    $this
      ->drupalPost('admin/config/search/path/add', $edit_node2, t('Save'));
    $edit = array(
      'optimizely_project_title' => $this
        ->randomName(8),
      'optimizely_project_code' => rand(0, 10000),
      'optimizely_path' => $edit_node2['alias'],
      'optimizely_enabled' => rand(0, 1),
    );
    $this
      ->drupalPost('admin/config/system/optimizely/add_update/2', $edit, t('Update'));

    // test if database was updated
    $project_title = db_query('SELECT project_title FROM {optimizely} WHERE project_title = :optimizely_project_title', array(
      ':optimizely_project_title' => $edit['optimizely_project_title'],
    ))
      ->fetchField();
    $this
      ->assertEqual($project_title, $edit['optimizely_project_title'], t('The project was updated in the database.'));
  }

}

/**
 * Web Tests
 */
class OptimizelyTestUserPermissionsCase extends DrupalWebTestCase {
  protected $privileged_user;

  /**
   * OptimizelyTestUserPermissionsCase getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'User Permissions Test',
      'description' => 'Ensure user permissons function properly.',
      'group' => 'Optimizely',
    );
  }

  /**
   * OptimizelyTestUserPermissionsCase setUp().
   */
  public function setUp() {
    parent::setUp('optimizely');

    // Enable any modules required for the test
    // Create and log in our user. The user has the arbitrary privilege
    // 'extra special edit any Optimizely project' which the code uses
    // to grant access.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'administer optimizely',
    ));
    $this
      ->drupalLogin($this->privileged_user);
  }
  public function testUserPermissions() {
    $permissions = array(
      'name' => 'administer optimizely',
    );
    $valid = $this
      ->checkPermissions($permissions);
    $this
      ->assertTrue($valid, $permissions['name'] . t(' is a valid permission.'));
    $this
      ->drupalGet('admin/config/system/optimizely');
    $this
      ->assertResponse(200, t('access allowed'));
    $this
      ->drupalLogout();
    $this
      ->drupalGet('admin/config/system/optimizely');
    $this
      ->assertResponse(403, t('access denied'));
  }

}

/**
 * Test schema creation.
 */
class OptimizelyTestSchemaCase extends DrupalWebTestCase {
  protected $privileged_user;

  /**
   * OptimizelyTestSchemaCase getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Schema Creation Test',
      'description' => 'Ensure schema creation.',
      'group' => 'Optimizely',
    );
  }

  /**
   * OptimizelyTestSchemaCase setUp().
   */
  public function setUp() {
    parent::setUp('optimizely');

    // Enable any modules required for the test
    // Create and log in our user. The user has the arbitrary privilege
    // 'extra special edit any Optimizely project' which the code uses
    // to grant access.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'administer optimizely',
    ));
    $this
      ->drupalLogin($this->privileged_user);
  }
  public function testOptimizelySchema() {
    $schema = module_invoke('optimizely', 'schema');
    $this
      ->assertNotNull($schema, t('Optimizely table was created.'));
  }

}

/**
 * Test enabling / disabling non-default project from update page
 */
class OptimizelyTestEnableDisableCase extends DrupalWebTestCase {
  protected $privileged_user;

  /**
   * OptimizelyTestEnableDisableCase getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Enable / Disable Project Test',
      'description' => 'Test enabling / disabling non-default projects.',
      'group' => 'Optimizely',
    );
  }

  /**
   * OptimizelyTestEnableDisableCase setUp().
   */
  public function setUp() {
    parent::setUp('optimizely');

    // Enable any modules required for the test
    // Create and log in our user. The user has the arbitrary privilege
    // 'extra special edit any Optimizely project' which the code uses
    // to grant access.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'administer optimizely',
      'create page content',
      'edit own page content',
      'administer url aliases',
      'create url aliases',
    ));
    $this
      ->drupalLogin($this->privileged_user);
  }
  public function testOptimizelyUpdateEnableDisable() {

    // create page
    $settings = array(
      'type' => 'page',
      'title' => $this
        ->randomName(32),
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            $this
              ->randomName(64),
          ),
        ),
      ),
    );
    $node = $this
      ->drupalCreateNode($settings);

    // Create the url alias
    $edit_node = array();
    $edit_node['source'] = 'node/' . $node->nid;
    $edit_node['alias'] = $this
      ->randomName(10);
    $this
      ->drupalPost('admin/config/search/path/add', $edit_node, t('Save'));

    // create a disabled project
    $edit = array(
      'optimizely_project_title' => $this
        ->randomName(8),
      'optimizely_project_code' => rand(0, 10000),
      'optimizely_path' => $edit_node['alias'],
      'optimizely_enabled' => 0,
    );
    $this
      ->drupalPost('admin/config/system/optimizely/add_update', $edit, t('Add'));
    $edit = array(
      'optimizely_enabled' => 1,
    );
    $this
      ->drupalPost('admin/config/system/optimizely/add_update/2', $edit, t('Update'));

    // test if project was enabled
    $enabled = db_query('SELECT enabled FROM {optimizely} WHERE oid = 2')
      ->fetchField();
    $this
      ->assertEqual($enabled, $edit['optimizely_enabled'], t('The project was enabled from update page.'));
    $edit = array(
      'optimizely_enabled' => 0,
    );
    $this
      ->drupalPost('admin/config/system/optimizely/add_update/2', $edit, t('Update'));

    // test if project was disabled
    $enabled = db_query('SELECT enabled FROM {optimizely} WHERE oid = 2')
      ->fetchField();
    $this
      ->assertEqual($enabled, $edit['optimizely_enabled'], t('The project was disabled from update page.'));
  }

}

/**
 * Test if the default project settings work correctly.
 */
class OptimizelyTestDefaultSettingsCase extends DrupalWebTestCase {
  protected $privileged_user;

  /**
   * OptimizelyTestDefaultSettingsCase getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Default Settings Test',
      'description' => 'Ensure ensure the project settings work correctly.',
      'group' => 'Optimizely',
    );
  }

  /**
   * OptimizelyTestDefaultSettingsCase setUp().
   */
  public function setUp() {
    parent::setUp('optimizely');

    // Enable any modules required for the test
    // Create and log in our user. The user has the arbitrary privilege
    // 'extra special edit any Optimizely project' which the code uses
    // to grant access.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'administer optimizely',
    ));
    $this
      ->drupalLogin($this->privileged_user);
  }

  // Test add the optimizely account ID to the Default project and enable/disable the Default project
  public function testOptimizelyDefaultSettings() {

    // add the Optimizely account ID
    $edit = array(
      'optimizely_id' => rand(0, 10000),
    );
    $this
      ->drupalPost('admin/config/system/optimizely/settings', $edit, t('Submit'));
    $optimizely_id = db_query('SELECT project_code FROM {optimizely} WHERE oid = 1')
      ->fetchField();
    $this
      ->assertEqual($optimizely_id, $edit['optimizely_id'], t('Optimizely ID number added to Default project.'));

    // enable the default project
    $edit = array(
      'optimizely_enabled' => 1,
    );
    $this
      ->drupalPost('admin/config/system/optimizely/add_update/1', $edit, t('Update'));
    $enabled = db_query('SELECT enabled FROM {optimizely} WHERE oid = 1')
      ->fetchField();
    $this
      ->assertEqual($enabled, $edit['optimizely_enabled'], t('The Default project was enabled.'));

    // disable the default project
    $edit = array(
      'optimizely_enabled' => 0,
    );
    $this
      ->drupalPost('admin/config/system/optimizely/add_update/1', $edit, t('Update'));
    $enabled = db_query('SELECT enabled FROM {optimizely} WHERE oid = 1')
      ->fetchField();
    $this
      ->assertEqual($enabled, $edit['optimizely_enabled'], t('The Default project was disabled.'));
  }

}

/**
 * Test if snippet is included in path when not using aliases.
 */
class OptimizelyTestIncludeSnippetCase extends DrupalWebTestCase {
  protected $privileged_user;

  /**
   * OptimizelyTestIncludeSnippetCase getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Include Snippet Test',
      'description' => 'Ensure that the Optimizely snippet included in project path when not using aliases.',
      'group' => 'Optimizely',
    );
  }

  /**
   * OptimizelyTestIncludeSnippetCase setUp().
   */
  public function setUp() {
    parent::setUp('optimizely');

    // Enable any modules required for the test
    // Create and log in our user. The user has the arbitrary privilege
    // 'extra special edit any Optimizely project' which the code uses
    // to grant access.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'administer optimizely',
      'create page content',
      'edit own page content',
      'administer url aliases',
      'create url aliases',
    ));
    $this
      ->drupalLogin($this->privileged_user);
  }
  public function testSnippet() {

    // create first page
    $settings = array(
      'type' => 'page',
      'title' => $this
        ->randomName(32),
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            $this
              ->randomName(64),
          ),
        ),
      ),
    );
    $node1 = $this
      ->drupalCreateNode($settings);

    // create second page
    $settings = array(
      'type' => 'page',
      'title' => $this
        ->randomName(32),
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            $this
              ->randomName(64),
          ),
        ),
      ),
    );
    $node2 = $this
      ->drupalCreateNode($settings);

    // create third page
    $settings = array(
      'type' => 'page',
      'title' => $this
        ->randomName(32),
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            $this
              ->randomName(64),
          ),
        ),
      ),
    );
    $node3 = $this
      ->drupalCreateNode($settings);

    // create fourth page
    $settings = array(
      'type' => 'page',
      'title' => $this
        ->randomName(32),
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            $this
              ->randomName(64),
          ),
        ),
      ),
    );
    $node4 = $this
      ->drupalCreateNode($settings);

    //array holding project field values
    $edit = array(
      'optimizely_project_title' => $this
        ->randomName(8),
      'optimizely_project_code' => rand(0, 10000),
      'optimizely_path' => "node/" . $node1->nid . "\n" . "node/" . $node2->nid,
      'optimizely_enabled' => 1,
    );

    // create snippet
    $snippet = 'http://cdn.optimizely.com/js/' . $edit['optimizely_project_code'] . '.js';

    //create the project
    $this
      ->drupalPost('admin/config/system/optimizely/add_update', $edit, t('Add'));

    //log out to make sure optimizely_refresh_cache() works
    $this
      ->drupalLogout();

    // @todo check how to turn "cache pages for anonymous users" and "Aggregate JavaScript files" to on on Performance page
    // check if snippet does appears on project path page

    //$this->drupalGet($edit_node1['alias']);
    $this
      ->drupalGet("node/" . $node1->nid);
    $this
      ->drupalGetContent();
    $this
      ->assertRaw($snippet, 'snippet found in markup of project path page -> node/' . $node1->nid);
    $this
      ->drupalGet("node/" . $node2->nid);
    $this
      ->drupalGetContent();
    $this
      ->assertRaw($snippet, 'snippet found in markup of project path page -> node2/' . $node1->nid);

    // check if snippet does not appear on other project path pages
    $this
      ->drupalGet("node/" . $node3->nid);
    $this
      ->drupalGetContent();

    // @todo check if drupalGetContent() is necessary
    $this
      ->assertNoRaw($snippet, 'snippet not found in markup of other page');
    $this
      ->drupalGet("node/" . $node4->nid);
    $this
      ->drupalGetContent();
    $this
      ->assertNoRaw($snippet, 'snippet not found in markup of other page');
  }

}

/**
 * Test if snippet is included in path when an alias is used.
 */
class OptimizelyTestIncludeAliasSnippetCase extends DrupalWebTestCase {
  protected $privileged_user;

  /**
   * OptimizelyTestIncludeAliasSnippetCase getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Include Alias Snippet Test',
      'description' => 'Ensure that the Optimizely snippet included in project path when using aliases.',
      'group' => 'Optimizely',
    );
  }

  /**
   * OptimizelyTestIncludeAliasSnippetCase setUp().
   */
  public function setUp() {
    parent::setUp('optimizely');

    // Enable any modules required for the test
    // Create and log in our user. The user has the arbitrary privilege
    // 'extra special edit any Optimizely project' which the code uses
    // to grant access.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'administer optimizely',
      'create page content',
      'edit own page content',
      'administer url aliases',
      'create url aliases',
    ));
    $this
      ->drupalLogin($this->privileged_user);
  }
  public function testSnippet() {

    // create first page
    $settings = array(
      'type' => 'page',
      'title' => $this
        ->randomName(32),
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            $this
              ->randomName(64),
          ),
        ),
      ),
    );
    $node1 = $this
      ->drupalCreateNode($settings);

    // Create the url alias of the project to be added
    $edit_node1 = array();
    $edit_node1['source'] = 'node/' . $node1->nid;
    $edit_node1['alias'] = $this
      ->randomName(10);
    $alias_details = array(
      "source" => $edit_node1['source'],
      "alias" => $edit_node1['alias'],
    );
    path_save($alias_details);

    // create second page
    $settings = array(
      'type' => 'page',
      'title' => $this
        ->randomName(32),
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            $this
              ->randomName(64),
          ),
        ),
      ),
    );
    $node2 = $this
      ->drupalCreateNode($settings);

    // Create the url alias of the project to be added
    $edit_node2 = array();
    $edit_node2['source'] = 'node/' . $node2->nid;
    $edit_node2['alias'] = $this
      ->randomName(10);
    $alias_details = array(
      "source" => $edit_node2['source'],
      "alias" => $edit_node2['alias'],
    );
    path_save($alias_details);

    // create third page
    $settings = array(
      'type' => 'page',
      'title' => $this
        ->randomName(32),
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            $this
              ->randomName(64),
          ),
        ),
      ),
    );
    $node3 = $this
      ->drupalCreateNode($settings);

    // Create the url alias
    $edit_node3 = array();
    $edit_node3['source'] = 'node/' . $node3->nid;
    $edit_node3['alias'] = $this
      ->randomName(9);
    $alias_details = array(
      "source" => $edit_node3['source'],
      "alias" => $edit_node3['alias'],
    );
    path_save($alias_details);

    // create fourth page
    $settings = array(
      'type' => 'page',
      'title' => $this
        ->randomName(32),
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            $this
              ->randomName(64),
          ),
        ),
      ),
    );
    $node4 = $this
      ->drupalCreateNode($settings);

    // Create the url alias
    $edit_node4 = array();
    $edit_node4['source'] = 'node/' . $node4->nid;
    $edit_node4['alias'] = $this
      ->randomName(10);
    $alias_details = array(
      "source" => $edit_node4['source'],
      "alias" => $edit_node4['alias'],
    );
    path_save($alias_details);

    // array holding project field values
    $edit = array(
      'optimizely_project_title' => $this
        ->randomName(8),
      'optimizely_project_code' => rand(0, 10000),
      'optimizely_path' => $edit_node1['alias'] . "\n" . $edit_node2['alias'],
      'optimizely_path' => $edit_node1['alias'],
      'optimizely_enabled' => 1,
    );

    // Snippet to be found on project pages
    $snippet = 'http://cdn.optimizely.com/js/' . $edit['optimizely_project_code'] . '.js';

    //create the project
    $this
      ->drupalPost('admin/config/system/optimizely/add_update', $edit, t('Add'));

    // log out to make sure optimizely_refresh_cache() works
    $this
      ->drupalLogout();

    // @todo check how to turn "cache pages for anonymous users" and "Aggregate JavaScript files" to on on Performance page
    // check if snippet does appears on project path page
    $this
      ->drupalGet($edit_node1['alias']);
    $this
      ->assertRaw($snippet, 'Snippet (' . $snippet . ') found in markup of project path page (alias) -> ' . $edit_node1['alias']);

    // check if snippet does appears on project path page
    $this
      ->drupalGet($edit_node2['alias']);
    $this
      ->assertRaw($snippet, 'Snippet (' . $snippet . ') found in markup of project path page (alias) -> ' . $edit_node2['alias']);

    // check if snippet does not appear on other project path pages
    $this
      ->drupalGet($edit_node3['alias']);
    $this
      ->assertNoRaw($snippet, 'Snippet (' . $snippet . ') <strong>**not**</strong> found in markup of other page -> ' . $edit_node3['alias']);
    $this
      ->drupalGet($edit_node4['alias']);
    $this
      ->assertNoRaw($snippet, 'Snippet (' . $snippet . ') <strong>**not**</strong> found in markup of other page -> ' . $edit_node4['alias']);
  }

}

Classes

Namesort descending Description
OptimizelyTestAddUpdateCase
OptimizelyTestDefaultProjectTestCase OptimizelyTestDefaultProjectTestCase: Test that:
OptimizelyTestDefaultSettingsCase * Test if the default project settings work correctly.
OptimizelyTestEnableDisableCase Test enabling / disabling non-default project from update page
OptimizelyTestIncludeAliasSnippetCase * Test if snippet is included in path when an alias is used.
OptimizelyTestIncludeSnippetCase * Test if snippet is included in path when not using aliases.
OptimizelyTestPageSnippetTestCase
OptimizelyTestSchemaCase Test schema creation.
OptimizelyTestUserPermissionsCase Web Tests
OptimizelyTestUserRoleTestCase OptimizelyTestUserRoleTestCase: Create anonymous, authenticated and privileged user to test access to module related pages.