public function FieldWebTest::testAlterUrl

Tests rewriting the output to a link.

File

drupal/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php, line 177
Contains \Drupal\views\Tests\Handler\FieldWebTest.

Class

FieldWebTest
Tests fields from within a UI.

Namespace

Drupal\views\Tests\Handler

Code

public function testAlterUrl() {
  $view = views_get_view('test_view');
  $view
    ->setDisplay();
  $view
    ->initHandlers();
  $this
    ->executeView($view);
  $row = $view->result[0];
  $id_field = $view->field['id'];

  // Setup the general settings required to build a link.
  $id_field->options['alter']['make_link'] = TRUE;
  $id_field->options['alter']['path'] = $path = $this
    ->randomName();

  // Tests that the suffix/prefix appears on the output.
  $id_field->options['alter']['prefix'] = $prefix = $this
    ->randomName();
  $id_field->options['alter']['suffix'] = $suffix = $this
    ->randomName();
  $output = $id_field
    ->theme($row);
  $this
    ->assertSubString($output, $prefix);
  $this
    ->assertSubString($output, $suffix);
  unset($id_field->options['alter']['prefix']);
  unset($id_field->options['alter']['suffix']);
  $output = $id_field
    ->theme($row);
  $this
    ->assertSubString($output, $path, 'Make sure that the path is part of the output');

  // Some generic test code adapted from the UrlTest class, which tests
  // mostly the different options for the path.
  global $base_url, $script_path;
  foreach (array(
    FALSE,
    TRUE,
  ) as $absolute) {

    // Get the expected start of the path string.
    $base = ($absolute ? $base_url . '/' : base_path()) . $script_path;
    $absolute_string = $absolute ? 'absolute' : NULL;
    $alter =& $id_field->options['alter'];
    $alter['path'] = 'node/123';
    $expected_result = url('node/123', array(
      'absolute' => $absolute,
    ));
    $alter['absolute'] = $absolute;
    $result = $id_field
      ->theme($row);
    $this
      ->assertSubString($result, $expected_result);
    $expected_result = url('node/123', array(
      'fragment' => 'foo',
      'absolute' => $absolute,
    ));
    $alter['path'] = 'node/123#foo';
    $result = $id_field
      ->theme($row);
    $this
      ->assertSubString($result, $expected_result);
    $expected_result = url('node/123', array(
      'query' => array(
        'foo' => NULL,
      ),
      'absolute' => $absolute,
    ));
    $alter['path'] = 'node/123?foo';
    $result = $id_field
      ->theme($row);
    $this
      ->assertSubString($result, $expected_result);
    $expected_result = url('node/123', array(
      'query' => array(
        'foo' => 'bar',
        'bar' => 'baz',
      ),
      'absolute' => $absolute,
    ));
    $alter['path'] = 'node/123?foo=bar&bar=baz';
    $result = $id_field
      ->theme($row);
    $this
      ->assertSubString(decode_entities($result), decode_entities($expected_result));
    $expected_result = url('node/123', array(
      'query' => array(
        'foo' => NULL,
      ),
      'fragment' => 'bar',
      'absolute' => $absolute,
    ));
    $alter['path'] = 'node/123?foo#bar';
    $result = $id_field
      ->theme($row);

    // @fixme: The actual result is node/123?foo#bar so views has a bug here.
    // $this->assertSubStringExists(decode_entities($result), decode_entities($expected_result));
    $expected_result = url('<front>', array(
      'absolute' => $absolute,
    ));
    $alter['path'] = '<front>';
    $result = $id_field
      ->theme($row);
    $this
      ->assertSubString($result, $expected_result);
  }

  // Tests the replace spaces with dashes feature.
  $id_field->options['alter']['replace_spaces'] = TRUE;
  $id_field->options['alter']['path'] = $path = $this
    ->randomName() . ' ' . $this
    ->randomName();
  $output = $id_field
    ->theme($row);
  $this
    ->assertSubString($output, str_replace(' ', '-', $path));
  $id_field->options['alter']['replace_spaces'] = FALSE;
  $output = $id_field
    ->theme($row);

  // The url has a space in it, so to check we have to decode the url output.
  $this
    ->assertSubString(urldecode($output), $path);

  // Tests the external flag.
  // Switch on the external flag should output an external url as well.
  $id_field->options['alter']['external'] = TRUE;
  $id_field->options['alter']['path'] = $path = 'drupal.org';
  $output = $id_field
    ->theme($row);
  $this
    ->assertSubString($output, 'http://drupal.org');

  // Setup a not external url, which shouldn't lead to an external url.
  $id_field->options['alter']['external'] = FALSE;
  $id_field->options['alter']['path'] = $path = 'drupal.org';
  $output = $id_field
    ->theme($row);
  $this
    ->assertNotSubString($output, 'http://drupal.org');

  // Tests the transforming of the case setting.
  $id_field->options['alter']['path'] = $path = $this
    ->randomName();
  $id_field->options['alter']['path_case'] = 'none';
  $output = $id_field
    ->theme($row);
  $this
    ->assertSubString($output, $path);

  // Switch to uppercase and lowercase.
  $id_field->options['alter']['path_case'] = 'upper';
  $output = $id_field
    ->theme($row);
  $this
    ->assertSubString($output, strtoupper($path));
  $id_field->options['alter']['path_case'] = 'lower';
  $output = $id_field
    ->theme($row);
  $this
    ->assertSubString($output, strtolower($path));

  // Switch to ucfirst and ucwords.
  $id_field->options['alter']['path_case'] = 'ucfirst';
  $id_field->options['alter']['path'] = 'drupal has a great community';
  $output = $id_field
    ->theme($row);
  $this
    ->assertSubString($output, drupal_encode_path('Drupal has a great community'));
  $id_field->options['alter']['path_case'] = 'ucwords';
  $output = $id_field
    ->theme($row);
  $this
    ->assertSubString($output, drupal_encode_path('Drupal Has A Great Community'));
  unset($id_field->options['alter']['path_case']);

  // Tests the linkclass setting and see whether it actuall exists in the output.
  $id_field->options['alter']['link_class'] = $class = $this
    ->randomName();
  $output = $id_field
    ->theme($row);
  $elements = $this
    ->xpathContent($output, '//a[contains(@class, :class)]', array(
    ':class' => $class,
  ));
  $this
    ->assertTrue($elements);

  // @fixme link_class, alt, rel cannot be unset, which should be fixed.
  $id_field->options['alter']['link_class'] = '';

  // Tests the alt setting.
  $id_field->options['alter']['alt'] = $rel = $this
    ->randomName();
  $output = $id_field
    ->theme($row);
  $elements = $this
    ->xpathContent($output, '//a[contains(@title, :alt)]', array(
    ':alt' => $rel,
  ));
  $this
    ->assertTrue($elements);
  $id_field->options['alter']['alt'] = '';

  // Tests the rel setting.
  $id_field->options['alter']['rel'] = $rel = $this
    ->randomName();
  $output = $id_field
    ->theme($row);
  $elements = $this
    ->xpathContent($output, '//a[contains(@rel, :rel)]', array(
    ':rel' => $rel,
  ));
  $this
    ->assertTrue($elements);
  $id_field->options['alter']['rel'] = '';

  // Tests the target setting.
  $id_field->options['alter']['target'] = $target = $this
    ->randomName();
  $output = $id_field
    ->theme($row);
  $elements = $this
    ->xpathContent($output, '//a[contains(@target, :target)]', array(
    ':target' => $target,
  ));
  $this
    ->assertTrue($elements);
  unset($id_field->options['alter']['target']);
}