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

/**
 * @file
 * Definition of Drupal\search\Tests\SearchLanguageTest.
 */

namespace Drupal\search\Tests;

10
use Drupal\field\Entity\FieldStorageConfig;
11
use Drupal\language\Entity\ConfigurableLanguage;
12

13
/**
14 15 16
 * Tests advanced search with different languages added.
 *
 * @group search
17 18
 */
class SearchLanguageTest extends SearchTestBase {
19 20 21 22 23 24 25 26

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

27 28 29 30 31 32 33
  /**
   * Array of nodes available to search.
   *
   * @var \Drupal\node\NodeInterface[]
   */
  protected $searchableNodes;

34
  protected function setUp() {
35
    parent::setUp();
36 37

    // Create and login user.
38
    $test_user = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search', 'administer nodes', 'administer languages', 'access administration pages', 'administer site configuration'));
39
    $this->drupalLogin($test_user);
40 41

    // Add a new language.
42
    ConfigurableLanguage::createFromLangcode('es')->save();
43 44 45 46

    // Make the body field translatable. The title is already translatable by
    // definition. The parent class has already created the article and page
    // content types.
47 48 49
    $field_storage = FieldStorageConfig::loadByName('node', 'body');
    $field_storage->translatable = TRUE;
    $field_storage->save();
50 51 52 53 54 55 56

    // Create a few page nodes with multilingual body values.
    $default_format = filter_default_format();
    $nodes = array(
      array(
        'title' => 'First node en',
        'type' => 'page',
57
        'body' => array(array('value' => $this->randomMachineName(32), 'format' => $default_format)),
58 59 60 61 62
        'langcode' => 'en',
      ),
      array(
        'title' => 'Second node this is the Spanish title',
        'type' => 'page',
63
        'body' => array(array('value' => $this->randomMachineName(32), 'format' => $default_format)),
64 65 66 67 68
        'langcode' => 'es',
      ),
      array(
        'title' => 'Third node en',
        'type' => 'page',
69
        'body' => array(array('value' => $this->randomMachineName(32), 'format' => $default_format)),
70 71 72
        'langcode' => 'en',
      ),
    );
73
    $this->searchableNodes = [];
74
    foreach ($nodes as $setting) {
75
      $this->searchableNodes[] = $this->drupalCreateNode($setting);
76 77 78
    }

    // Add English translation to the second node.
79
    $translation = $this->searchableNodes[1]->addTranslation('en', array('title' => 'Second node en'));
80
    $translation->body->value = $this->randomMachineName(32);
81
    $this->searchableNodes[1]->save();
82 83

    // Add Spanish translation to the third node.
84
    $translation = $this->searchableNodes[2]->addTranslation('es', array('title' => 'Third node es'));
85
    $translation->body->value = $this->randomMachineName(32);
86
    $this->searchableNodes[2]->save();
87 88 89 90 91

    // Update the index and then run the shutdown method.
    $plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
    $plugin->updateIndex();
    search_update_totals();
92 93 94 95 96
  }

  function testLanguages() {
    // Add predefined language.
    $edit = array('predefined_langcode' => 'fr');
97
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
98
    $this->assertText('French', 'Language added successfully.');
99 100 101

    // Now we should have languages displayed.
    $this->drupalGet('search/node');
102 103 104
    $this->assertText(t('Languages'), 'Languages displayed to choose from.');
    $this->assertText(t('English'), 'English is a possible choice.');
    $this->assertText(t('French'), 'French is a possible choice.');
105 106

    // Ensure selecting no language does not make the query different.
107
    $this->drupalPostForm('search/node', array(), t('Advanced search'));
108
    $this->assertUrl(\Drupal::url('search.view_node_search', [], ['query' => ['keys' => ''], 'absolute' => TRUE]), [], 'Correct page redirection, no language filtering.');
109 110 111

    // Pick French and ensure it is selected.
    $edit = array('language[fr]' => TRUE);
112
    $this->drupalPostForm('search/node', $edit, t('Advanced search'));
113 114 115 116 117
    // Get the redirected URL.
    $url = $this->getUrl();
    $parts = parse_url($url);
    $query_string = isset($parts['query']) ? rawurldecode($parts['query']) : '';
    $this->assertTrue(strpos($query_string, '=language:fr') !== FALSE, 'Language filter language:fr add to the query string.');
118

119 120 121 122 123 124 125 126 127 128 129
    // Search for keyword node and language filter as Spanish.
    $edit = array('keys' => 'node', 'language[es]' => TRUE);
    $this->drupalPostForm('search/node', $edit, t('Advanced search'));
    // Check for Spanish results.
    $this->assertLink('Second node this is the Spanish title', 0, 'Second node Spanish title found in search results');
    $this->assertLink('Third node es', 0, 'Third node Spanish found in search results');
    // Ensure that results doesn't contain other language nodes.
    $this->assertNoLink('First node en', 'Search results does not contain first English node');
    $this->assertNoLink('Second node en', 'Search results does not contain second English node');
    $this->assertNoLink('Third node en', 'Search results does not contain third English node');

130
    // Change the default language and delete English.
131
    $path = 'admin/config/regional/settings';
132
    $this->drupalGet($path);
133 134 135 136
    $this->assertOptionSelected('edit-site-default-language', 'en', 'Default language updated.');
    $edit = array(
      'site_default_language' => 'fr',
    );
137
    $this->drupalPostForm($path, $edit, t('Save configuration'));
138
    $this->assertNoOptionSelected('edit-site-default-language', 'en', 'Default language updated.');
139
    $this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete'));
140 141
  }
}