TaxonomyTermViewTest.php 4.77 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\taxonomy\Tests\Views\TaxonomyTermViewTest.
 */

namespace Drupal\taxonomy\Tests\Views;

10
use Drupal\Component\Utility\Unicode;
11 12 13 14
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\user\Entity\Role;
15
use Drupal\views\Views;
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

/**
 * Tests the taxonomy term view page and its translation.
 *
 * @group taxonomy
 */
class TaxonomyTermViewTest extends TaxonomyTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('taxonomy', 'views');

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Create an administrative user.
    $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
    $this->drupalLogin($this->admin_user);

    // Create a vocabulary and add two term reference fields to article nodes.

43
    $this->field_name_1 = Unicode::strtolower($this->randomMachineName());
44
    entity_create('field_storage_config', array(
45
      'field_name' => $this->field_name_1,
46 47 48 49 50 51 52 53 54 55 56 57
      'entity_type' => 'node',
      'type' => 'taxonomy_term_reference',
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => $this->vocabulary->id(),
            'parent' => 0,
          ),
        ),
      ),
    ))->save();
58
    entity_create('field_config', array(
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
      'field_name' => $this->field_name_1,
      'bundle' => 'article',
      'entity_type' => 'node',
    ))->save();
    entity_get_form_display('node', 'article', 'default')
      ->setComponent($this->field_name_1, array(
        'type' => 'options_select',
      ))
      ->save();
    entity_get_display('node', 'article', 'default')
      ->setComponent($this->field_name_1, array(
        'type' => 'taxonomy_term_reference_link',
      ))
      ->save();
  }

  /**
   * Tests that the taxonomy term view is working properly.
   */
  public function testTaxonomyTermView() {
    // Create terms in the vocabulary.
    $term = $this->createTerm($this->vocabulary);

    // Post an article.
    $edit = array();
    $edit['title[0][value]'] = $original_title = $this->randomMachineName();
    $edit['body[0][value]'] = $this->randomMachineName();
    $edit["{$this->field_name_1}[]"] = $term->id();
    $this->drupalPostForm('node/add/article', $edit, t('Save'));
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);

    $this->drupalGet('taxonomy/term/' . $term->id());
    $this->assertText($term->label());
    $this->assertText($node->label());

94
    \Drupal::service('module_installer')->install(array('language', 'content_translation'));
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    $language = ConfigurableLanguage::createFromLangcode('ur');
    $language->save();
    // Enable translation for the article content type and ensure the change is
    // picked up.
    content_translation_set_config('node', 'article', 'enabled', TRUE);
    $roles = $this->admin_user->getRoles(TRUE);
    Role::load(reset($roles))
      ->grantPermission('create content translations')
      ->grantPermission('translate any entity')
      ->save();
    drupal_static_reset();
    \Drupal::entityManager()->clearCachedDefinitions();
    \Drupal::service('router.builder')->rebuild();

    $edit['title[0][value]'] = $translated_title = $this->randomMachineName();

    $this->drupalPostForm('node/' . $node->id() . '/translations/add/en/ur', $edit, t('Save (this translation)'));

    $this->drupalGet('taxonomy/term/' . $term->id());
    $this->assertText($term->label());
    $this->assertText($original_title);
    $this->assertNoText($translated_title);

    $this->drupalGet('ur/taxonomy/term/' . $term->id());
    $this->assertText($term->label());
    $this->assertNoText($original_title);
    $this->assertText($translated_title);
122 123 124 125

    // Uninstall language module and ensure that the language is not part of the
    // query anymore.
    // @see \Drupal\views\Plugin\views\filter\LanguageFilter::query()
126
    \Drupal::service('module_installer')->uninstall(['content_translation', 'language']);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

    $view = Views::getView('taxonomy_term');
    $view->initDisplay();
    $view->setArguments([$term->id()]);
    $view->build();
    /** @var \Drupal\Core\Database\Query\Select $query */
    $query = $view->build_info['query'];
    $tables = $query->getTables();

    // Ensure that the join to node_field_data is not added by default.
    $this->assertEqual(['node', 'taxonomy_index'], array_keys($tables));
    // Ensure that the filter to the language column is not there by default.
    $condition = $query->conditions();
    // We only want to check the no. of conditions in the query.
    unset($condition['#conjunction']);
    $this->assertEqual(1, count($condition));
143 144 145
  }

}