You are here

protected function GlobalRedirectTestCase::_globalredirect_batch_test in Global Redirect 7

Same name and namespace in other branches
  1. 6 globalredirect.test \GlobalRedirectTestCase::_globalredirect_batch_test()
4 calls to GlobalRedirectTestCase::_globalredirect_batch_test()
GlobalRedirectTestCaseConfigAlpha::testGlobalRedirect in ./globalredirect.test
GlobalRedirectTestCaseConfigBeta::testGlobalRedirect in ./globalredirect.test
GlobalRedirectTestCaseConfigLanguages::testGlobalRedirect in ./globalredirect.test
GlobalRedirectTestCaseDefault::testGlobalRedirect in ./globalredirect.test

File

./globalredirect.test, line 227
Global Redirect functionality tests

Class

GlobalRedirectTestCase
@file Global Redirect functionality tests

Code

protected function _globalredirect_batch_test() {

  // Define the default path options
  $path_defaults = array(
    'options' => array(),
  );
  $path_options_defaults = array(
    'base_url' => $GLOBALS['base_url'] . base_path(),
    'absolute' => TRUE,
    'alias' => TRUE,
    'external' => TRUE,
  );

  // Get the test paths
  $test_paths = $this
    ->_globalredirect_test_paths();
  $this
    ->assert('pass', '<pre>' . print_r($test_paths, TRUE) . '</pre>');

  // Foreach of the above, lets check they redirect correctly
  foreach ($test_paths as $id => $path) {

    // Overlay some defaults onto the path. This ensures 'options' is set as an array.
    $path += $path_defaults;

    // If the path doesn't have an absolute or alias setting, set them to TRUE.
    $path['options'] += $path_options_defaults;

    // Build a URL from the path
    $request_path = $path['options']['external'] ? $path['options']['base_url'] . $path['request'] : $path['request'];
    $request_path = url($request_path, $path['options']);

    // Display a message telling the user what we're testing
    $info = array(
      '!id' => $id,
      '!path' => '<pre>' . print_r($path, TRUE) . '</pre>',
      '!url' => $request_path,
    );
    $this
      ->pass(t(INFO_MESSAGE, $info), 'GlobalRedirect');

    // Do a HEAD request (don't care about the body). The alias=>TRUE is to tell Drupal not to lookup the alias - this is a raw request.
    $this
      ->drupalHead($request_path, array(
      'alias' => TRUE,
    ));

    // Grab the headers from the request
    $headers = $this
      ->drupalGetHeaders(TRUE);

    // Build a nice array of results
    $result = array(
      '!expected_status' => $path['return-code'],
      '!location' => isset($headers[0]['location']) ? $headers[0]['location'] : 'N/A',
      '!status' => $headers[0][':status'],
    );

    // If the expected result is not a redirect, then there is no expected path in the location header.
    if ($path['return-code'] != 301) {
      $result['!expected_path'] = 'N/A';
    }
    else {
      $url_options = array(
        'absolute' => TRUE,
      );
      if (isset($path['expected-path'])) {

        // If we have an expected path provided, use this and tell url() not to do an alias lookup
        $expected = $path['expected-path'];
        $url_options['alias'] = TRUE;
      }
      else {

        // Otherwise set the path to the request and let url() do an alias lookup.
        $expected = $path['request'];
      }
      $result['!expected_path'] = url($expected, $url_options);
    }

    // First test - is the status as expected? (Note: The expected status must be cast to string for strpos to work)
    if (strpos($result['!status'], (string) $result['!expected_status']) !== FALSE) {

      // Ok, we have a status and the status contains the appropriate response code (eg, 200, 301, 403 or 404).
      // Next test (if expected return code is 301) - is the location set, and is it as expected?
      if ($result['!expected_status'] == 301 && $result['!location'] == $result['!expected_path']) {

        // We have redirect and ended up in the right place - a PASS!!!
        $this
          ->pass(t(SUCCESS_MESSAGE, $result), 'GlobalRedirect');
      }
      elseif ($result['!expected_status'] != 301) {

        // We weren't supposed to redirect - this is good!
        $this
          ->pass(t(SUCCESS_MESSAGE, $result), 'GlobalRedirect');
      }
      else {

        // In this case either the return-code or the returned location is unexpected
        $this
          ->fail(t(ERROR_MESSAGE, $result), 'GlobalRedirect');
        $this
          ->fail('<pre>' . print_r($headers, TRUE) . '</pre>');
      }
    }
    else {

      // The status either wasn't present or was not as expected
      $this
        ->fail(t(ERROR_MESSAGE, $result), 'GlobalRedirect');
      $this
        ->fail('<pre>' . print_r($headers, TRUE) . '</pre>');
    }
  }
}