SearchLanguageTest.php 5.44 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
  protected function setUp() {
28
    parent::setUp();
29 30

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

    // Add a new language.
35
    ConfigurableLanguage::createFromLangcode('es')->save();
36 37 38 39

    // Make the body field translatable. The title is already translatable by
    // definition. The parent class has already created the article and page
    // content types.
40 41 42
    $field_storage = FieldStorageConfig::loadByName('node', 'body');
    $field_storage->translatable = TRUE;
    $field_storage->save();
43 44 45 46 47 48 49

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

    // Add English translation to the second node.
    $translation = $this->searchable_nodes[1]->addTranslation('en', array('title' => 'Second node en'));
73
    $translation->body->value = $this->randomMachineName(32);
74 75 76 77
    $this->searchable_nodes[1]->save();

    // Add Spanish translation to the third node.
    $translation = $this->searchable_nodes[2]->addTranslation('es', array('title' => 'Third node es'));
78
    $translation->body->value = $this->randomMachineName(32);
79 80 81 82 83 84
    $this->searchable_nodes[2]->save();

    // Update the index and then run the shutdown method.
    $plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
    $plugin->updateIndex();
    search_update_totals();
85 86 87 88 89
  }

  function testLanguages() {
    // Add predefined language.
    $edit = array('predefined_langcode' => 'fr');
90
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
91
    $this->assertText('French', 'Language added successfully.');
92 93 94

    // Now we should have languages displayed.
    $this->drupalGet('search/node');
95 96 97
    $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.');
98 99

    // Ensure selecting no language does not make the query different.
100
    $this->drupalPostForm('search/node', array(), t('Advanced search'));
101
    $this->assertEqual($this->getUrl(), url('search/node/', array('query' => array('keys' => ''), 'absolute' => TRUE)), 'Correct page redirection, no language filtering.');
102 103 104

    // Pick French and ensure it is selected.
    $edit = array('language[fr]' => TRUE);
105
    $this->drupalPostForm('search/node', $edit, t('Advanced search'));
106 107 108 109 110
    // 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.');
111

112 113 114 115 116 117 118 119 120 121 122
    // 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');

123
    // Change the default language and delete English.
124
    $path = 'admin/config/regional/settings';
125
    $this->drupalGet($path);
126 127 128 129
    $this->assertOptionSelected('edit-site-default-language', 'en', 'Default language updated.');
    $edit = array(
      'site_default_language' => 'fr',
    );
130
    $this->drupalPostForm($path, $edit, t('Save configuration'));
131
    $this->assertNoOptionSelected('edit-site-default-language', 'en', 'Default language updated.');
132
    $this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete'));
133 134
  }
}