LanguageNegotiationUrlTest.php 4.19 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Tests\language\Unit\LanguageNegotiationUrlTest.
6 7
 */

8
namespace Drupal\Tests\language\Unit;
9 10 11 12 13 14 15

use Drupal\Core\Session\UserSession;
use Drupal\Tests\UnitTestCase;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Symfony\Component\HttpFoundation\Request;

/**
16 17
 * @coversDefaultClass \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl
 * @group language
18 19 20 21 22 23 24 25 26
 */
class LanguageNegotiationUrlTest extends UnitTestCase {

  protected $languageManager;
  protected $user;

  /**
   * {@inheritdoc}
   */
27
  protected function setUp() {
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

    // Set up some languages to be used by the language-based path processor.
    $languages = array(
      'de' => (object) array(
        'id' => 'de',
      ),
      'en' => (object) array(
        'id' => 'en',
      ),
    );

    // Create a language manager stub.
    $language_manager = $this->getMockBuilder('Drupal\language\ConfigurableLanguageManagerInterface')
      ->getMock();
    $language_manager->expects($this->any())
      ->method('getCurrentLanguage')
      ->will($this->returnValue($languages['en']));
    $language_manager->expects($this->any())
      ->method('getLanguages')
      ->will($this->returnValue($languages));
    $this->languageManager = $language_manager;

    // Create a user stub.
    $this->user = $this->getMockBuilder('Drupal\Core\Session\AccountInterface')
      ->getMock();
  }

  /**
   * Test domain language negotiation.
   *
   * @dataProvider providerTestDomain
   */
  public function testDomain($http_host, $domains, $expected_langcode) {
    $config_data = array(
      'source' => LanguageNegotiationUrl::CONFIG_DOMAIN,
      'domains' => $domains,
    );

    $config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config_object->expects($this->any())
      ->method('get')
      ->with('url')
      ->will($this->returnValue($config_data));

74
    $config = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    $config->expects($this->any())
      ->method('get')
      ->with('language.negotiation')
      ->will($this->returnValue($config_object));

    $request = Request::create('', 'GET', array(), array(), array(), array('HTTP_HOST' => $http_host));
    $method = new LanguageNegotiationUrl();
    $method->setLanguageManager($this->languageManager);
    $method->setConfig($config);
    $method->setCurrentUser($this->user);
    $this->assertEquals($expected_langcode, $method->getLangcode($request));
  }

  /**
   * Provides data for the domain test.
   *
   * @return array
   *   An array of data for checking domain negotation.
   */
  public function providerTestDomain() {

    $domain_configuration[] = array(
      'http_host' => 'example.de',
      'domains' => array(
        'de' => 'http://example.de',
      ),
101
      'expected_langcode' => 'de',
102 103 104 105 106
    );
    // No configuration.
    $domain_configuration[] = array(
      'http_host' => 'example.de',
      'domains' => array(),
107
      'expected_langcode' => FALSE,
108 109 110 111 112 113 114
    );
    // HTTP host with a port.
    $domain_configuration[] = array(
      'http_host' => 'example.de:8080',
      'domains' => array(
        'de' => 'http://example.de',
      ),
115
      'expected_langcode' => 'de',
116 117 118 119 120 121 122
    );
    // Domain configuration with https://.
    $domain_configuration[] = array(
      'http_host' => 'example.de',
      'domains' => array(
        'de' => 'https://example.de',
      ),
123
      'expected_langcode' => 'de',
124 125 126 127 128 129 130
    );
    // Non-matching HTTP host.
    $domain_configuration[] = array(
      'http_host' => 'example.com',
      'domains' => array(
        'de' => 'http://example.com',
      ),
131
      'expected_langcode' => 'de',
132 133 134 135 136 137 138
    );
    // Testing a non-existing language.
    $domain_configuration[] = array(
      'http_host' => 'example.com',
      'domains' => array(
        'it' => 'http://example.it',
      ),
139
      'expected_langcode' => FALSE,
140 141 142 143 144 145 146 147
    );
    // Multiple domain configurations.
    $domain_configuration[] = array(
      'http_host' => 'example.com',
      'domains' => array(
        'de' => 'http://example.de',
        'en' => 'http://example.com',
      ),
148
      'expected_langcode' => 'en',
149 150 151 152
    );
    return $domain_configuration;
  }
}