Tests the glossary feature.

File

tests/views_glossary.test, line 27
Definition of ViewsGlossaryTestCase.

Class

ViewsGlossaryTestCase
Tests the glossary feature.

Code

public function testGlossaryView() {

  // Create a content type and add some nodes, with a non random title.
  $type = $this
    ->drupalCreateContentType();
  $nodes_per_character = array(
    'd' => 1,
    'r' => 4,
    'u' => 10,
    'p' => 2,
    'a' => 3,
    'l' => 6,
  );
  $published = array();
  $unpublished = array();
  foreach ($nodes_per_character as $character => $count) {
    $setting = array(
      'type' => $type->type,
    );
    for ($i = 0; $i < $count + 1; $i++) {
      $node = $setting;
      $node['title'] = $character . strtolower($this
        ->randomName());

      // Make the first node unpublished, which will allow testing to make
      // sure that only published nodes are displayed. Also, don't add it to
      // the array of expected nodes.
      if ($i === 0) {
        $node['status'] = 0;
        $test_node = $this
          ->drupalCreateNode($node);
        $unpublished[$test_node->nid] = $test_node;
      }
      else {
        $published[$character][] = $this
          ->drupalCreateNode($node);
      }
    }
  }

  // Verify that there are the same number of unpublished nodeas as there are
  // letters.
  $this
    ->assertEqual(count($unpublished), count($nodes_per_character));

  // Verify that each unpublished item is a full node and that each one starts
  // with a letter from the list.
  foreach ($unpublished as $node) {
    $this
      ->assertTrue(is_object($node));
    $this
      ->assertTrue(!empty($node->nid));
    $first_letter = substr($node->title, 0, 1);
    $this
      ->assertTrue(isset($nodes_per_character[$first_letter]));
  }

  // Sort created nodes the same way the view does, so that we can assert
  // correct node ids for each row in the result set later.
  foreach ($nodes_per_character as $character => $count) {
    usort($published[$character], function ($a, $b) {
      return strcmp($a->title, $b->title);
    });
  }

  // Execute glossary view.
  $view = views_get_view('glossary');
  $view
    ->set_display('attachment');
  $view
    ->execute_display('attachment');

  // Check the amount of nodes per character - note that the unpublished nodes
  // will not be included.
  foreach ($view->result as $item) {
    $this
      ->assertEqual($nodes_per_character[$item->title_truncated], $item->num_records);
  }
  $view
    ->destroy();

  // Checks that a glossary view with an argument containing one letter
  // returns only and all the nodes that start with that letter.
  $view = views_get_view('glossary');
  $view
    ->init_display();
  $this
    ->executeView($view, array(
    'a',
  ));
  $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
  $this
    ->assertIdentical($result_count, 3, 'View has 3 results.');
  foreach ($view->result as $delta => $item) {
    $nid = isset($view->result[$delta]->nid) ? $view->result[$delta]->nid : '0';
    $this
      ->assertIdentical($nid, $published['a'][$delta]->nid, 'View result ' . (string) (int) $delta . ' has correct node id.');
    $this
      ->assertFalse(isset($unpublished[$nid]));
  }
  $view
    ->destroy();

  // Checks that a glossary view with an argument containing multiple values
  // returns only and all nodes that start with these values.
  $view = views_get_view('glossary');
  $view
    ->init_display();
  $arguments = $view->display_handler
    ->get_option('arguments');
  $arguments['title']['break_phrase'] = TRUE;
  $view->display_handler
    ->set_option('arguments', $arguments);
  $this
    ->executeView($view, array(
    'd+p',
  ));
  $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
  $this
    ->assertIdentical($result_count, 3, 'View has 3 results.');
  $nid = isset($view->result[0]->nid) ? $view->result[0]->nid : '0';
  $this
    ->assertIdentical($nid, $published['d'][0]->nid, 'View result 0 has correct node id.');
  $nid = isset($view->result[1]->nid) ? $view->result[1]->nid : '0';
  $this
    ->assertIdentical($nid, $published['p'][0]->nid, 'View result 1 has correct node id.');
  $nid = isset($view->result[2]->nid) ? $view->result[2]->nid : '0';
  $this
    ->assertIdentical($nid, $published['p'][1]->nid, 'View result 2 has correct node id.');
  $view
    ->destroy();

  // Checks that a glossary view with a phrase as an argument does not
  // interpret that phrase as multiple values.
  $view = views_get_view('glossary');
  $view
    ->init_display();
  $arguments = $view->display_handler
    ->get_option('arguments');
  $view->display_handler
    ->set_option('arguments', $arguments);
  $this
    ->executeView($view, array(
    'd+p',
  ));
  $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 1;
  $this
    ->assertIdentical($result_count, 0, 'View has zero results.');
  $view
    ->destroy();

  // Checks that a glossary view with an argument containing one letter
  // excludes all nodes that start with that letter.
  $view = views_get_view('glossary');
  $view
    ->init_display();
  $arguments = $view->display_handler
    ->get_option('arguments');
  $arguments['title']['not'] = TRUE;
  $view->display_handler
    ->set_option('arguments', $arguments);
  $this
    ->executeView($view, array(
    'a',
  ));
  $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
  $this
    ->assertIdentical($result_count, 23, 'View has 23 results.');
  $character = 'd';
  $character_delta = 0;
  foreach ($view->result as $delta => $item) {
    if ($delta === 1) {
      $character = 'l';
      $character_delta = 0;
    }
    elseif ($delta === 7) {
      $character = 'p';
      $character_delta = 0;
    }
    elseif ($delta === 9) {
      $character = 'r';
      $character_delta = 0;
    }
    elseif ($delta === 13) {
      $character = 'u';
      $character_delta = 0;
    }
    $nid = isset($view->result[$delta]->nid) ? $view->result[$delta]->nid : '0';
    $this
      ->assertIdentical($nid, $published[$character][$character_delta]->nid, 'View result ' . (string) (int) $delta . ' has correct node id.');
    $character_delta++;
  }
  $view
    ->destroy();

  // Checks that a glossary view with an argument containing multiple values
  // excludes all nodes that start with these values.
  $view = views_get_view('glossary');
  $view
    ->init_display();
  $arguments = $view->display_handler
    ->get_option('arguments');
  $arguments['title']['break_phrase'] = TRUE;
  $arguments['title']['not'] = TRUE;
  $view->display_handler
    ->set_option('arguments', $arguments);
  $this
    ->executeView($view, array(
    'a+p',
  ));
  $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
  $this
    ->assertIdentical($result_count, 21, 'View has 21 results.');
  $character = 'd';
  $character_delta = 0;
  foreach ($view->result as $delta => $item) {
    if ($delta === 1) {
      $character = 'l';
      $character_delta = 0;
    }
    elseif ($delta === 7) {
      $character = 'r';
      $character_delta = 0;
    }
    elseif ($delta === 11) {
      $character = 'u';
      $character_delta = 0;
    }
    $nid = isset($view->result[$delta]->nid) ? $view->result[$delta]->nid : '0';
    $this
      ->assertIdentical($nid, $published[$character][$character_delta]->nid, 'View result ' . (string) (int) $delta . ' has correct node id.');
    $character_delta++;
  }
  $view
    ->destroy();
}