Skip to content
GlossaryTest.php 3.51 KiB
Newer Older
Earl Miles's avatar
Earl Miles committed
<?php

namespace Drupal\views\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\LanguageInterface;
Earl Miles's avatar
Earl Miles committed
/**
 * Tests glossary functionality of views.
 *
 * @group views
class GlossaryTest extends ViewTestBase {
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['node'];
Earl Miles's avatar
Earl Miles committed
  /**
   * Tests the default glossary view.
   */
  public function testGlossaryView() {
    // Create a content type and add some nodes, with a non-random title.
Earl Miles's avatar
Earl Miles committed
    $type = $this->drupalCreateContentType();
Earl Miles's avatar
Earl Miles committed
      'd' => 1,
      'r' => 4,
      'u' => 10,
      'p' => 2,
      'a' => 3,
      'l' => 6,
Earl Miles's avatar
Earl Miles committed
    foreach ($nodes_per_char as $char => $count) {
Earl Miles's avatar
Earl Miles committed
      for ($i = 0; $i < $count; $i++) {
        $node = $setting;
        $node['title'] = $char . $this->randomString(3);
        $node = $this->drupalCreateNode($node);
        $nodes_by_char[$char][] = $node;
Earl Miles's avatar
Earl Miles committed
      }
    }

    // Execute glossary view
    $view->setDisplay('attachment_1');
    $view->executeDisplay('attachment_1');
Earl Miles's avatar
Earl Miles committed

    // Check that the amount of nodes per char.
    foreach ($view->result as $item) {
      $this->assertEqual($nodes_per_char[$item->title_truncated], $item->num_records);
    }

    // Enable the glossary to be displayed.
    $view->storage->enable()->save();
    $this->container->get('router.builder')->rebuildIfNeeded();
    $url = Url::fromRoute('view.glossary.page_1');

    // Verify cache tags.
    $this->assertPageCacheContextsAndTags(
      $url,
      [
        'languages:' . LanguageInterface::TYPE_CONTENT,
        'languages:' . LanguageInterface::TYPE_INTERFACE,
        'theme',
        'url',
        'user.node_grants:view',
        'user.permissions',
        'route',
      ],
      [
        'config:views.view.glossary',
        'node:' . $nodes_by_char['a'][0]->id(), 'node:' . $nodes_by_char['a'][1]->id(), 'node:' . $nodes_by_char['a'][2]->id(),
        // Link for letter 'd'.
        'node:1',
        // Link for letter 'p'.
        'node:16',
        // Link for letter 'r'.
        'node:2',
        // Link for letter 'l'.
        'node:21',
        // Link for letter 'u'.
        'node:6',
        'rendered',
        // FinishResponseSubscriber adds this cache tag to responses that have the
        // 'user.permissions' cache context for anonymous users.
        'config:user.role.anonymous',
      ]
    );
    // Check the actual page response.
    $this->assertResponse(200);
    foreach ($nodes_per_char as $char => $count) {
      $href = Url::fromRoute('view.glossary.page_1', ['arg_0' => $char])->toString();
      $label = Unicode::strtoupper($char);
      // Get the summary link for a certain character. Filter by label and href
      // to ensure that both of them are correct.
      $result = $this->xpath('//a[contains(@href, :href) and normalize-space(text())=:label]/..', [':href' => $href, ':label' => $label]);
      $this->assertTrue(count($result));
      // The rendered output looks like "| (count)" so let's figure out the int.
      $result_count = trim(str_replace(['|', '(', ')'], '', (string) $result[0]));
      $this->assertEqual($result_count, $count, 'The expected number got rendered.');
    }