Completed
Pull Request — 8.x-3.x (#362)
by Philipp
03:07
created

LanguageTest::testLanguageSwitchLinks()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\Tests\graphql_core\Kernel\Languages;
4
5
use Drupal\language\Entity\ConfigurableLanguage;
6
use Drupal\Tests\graphql_core\Kernel\GraphQLCoreTestBase;
7
8
/**
9
 * Test multilingual behavior of `graphql_core` features.
10
 *
11
 * @group graphql_core
12
 */
13
class LanguageTest extends GraphQLCoreTestBase {
14
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public static $modules = [
19
    'language',
20
    'graphql_context_test',
21
  ];
22
23
  /**
24
   * {@inheritdoc}
25
   */
26
  protected function setUp() {
27
    parent::setUp();
28
29
    $this->installconfig(['language']);
30
    $this->installEntitySchema('configurable_language');
31
    $this->container->get('router.builder')->rebuild();
32
33
    ConfigurableLanguage::create([
34
      'id' => 'fr',
35
      'weight' => 1,
36
    ])->save();
37
38
    ConfigurableLanguage::create([
39
      'id' => 'es',
40
      'weight' => 2,
41
    ])->save();
42
43
    ConfigurableLanguage::create([
44
      'id' => 'pt-br',
45
      'weight' => 3,
46
    ])->save();
47
48
    $config = $this->config('language.negotiation');
49
    $config->set('url.prefixes', ['en' => 'en', 'es' => 'es', 'fr' => 'fr'])
50
      ->save();
51
52
    $this->container->get('kernel')->rebuildContainer();
53
  }
54
55
  /**
56
   * Test listing of available languages.
57
   */
58
  public function testLanguageId() {
59
    // TODO: Check cache metadata.
60
    $metadata = $this->defaultCacheMetaData();
61
62
    $this->assertResults($this->getQueryFromFile('languages.gql'), [], [
63
      'languages' => [
64
        0 => [
65
          'id' => 'en',
66
          'name' => 'English',
67
          'isDefault' => TRUE,
68
          'isLocked' => FALSE,
69
          'direction' => 'ltr',
70
          'weight' => 0,
71
          'argument' => 'en',
72
        ],
73
74
        1 => [
75
          'id' => 'fr',
76
          'name' => 'French',
77
          'isDefault' => FALSE,
78
          'isLocked' => FALSE,
79
          'direction' => 'ltr',
80
          'weight' => 1,
81
          'argument' => 'fr',
82
        ],
83
84
        2 => [
85
          'id' => 'es',
86
          'name' => 'Spanish',
87
          'isDefault' => FALSE,
88
          'isLocked' => FALSE,
89
          'direction' => 'ltr',
90
          'weight' => 2,
91
          'argument' => 'es',
92
        ],
93
        3 => [
94
          'id' => 'pt-br',
95
          'name' => 'Portuguese, Brazil',
96
          'isDefault' => FALSE,
97
          'isLocked' => FALSE,
98
          'direction' => 'ltr',
99
          'weight' => 3,
100
          'argument' => 'pt_br',
101
        ],
102
      ],
103
    ], $metadata);
104
  }
105
106
  /**
107
   * Test language switch links.
108
   */
109
  public function testLanguageSwitchLinks() {
110
    $result = $this->executeQueryFile('languages.gql');
111
112
    $english = [
113
      'langcode' => 'en',
114
      'url' => [
115
        'asString' => '/en',
116
      ],
117
      'title' => 'English',
118
      'isActive' => true,
119
    ];
120
121
    $french = [
122
      'langcode' => 'fr',
123
      'url' => [
124
        'asString' => '/fr',
125
      ],
126
      'title' => NULL,
127
      'isActive' => false,
128
    ];
129
130
    $spanish = [
131
      'langcode' => 'es',
132
      'url' => [
133
        'asString' => '/es',
134
      ],
135
      'title' => NULL,
136
      'isActive' => false,
137
    ];
138
139
    $brazil = [
140
      'langcode' => 'pt-br',
141
      'url' => [
142
        'asString' => '/',
143
      ],
144
      'title' => NULL,
145
      'isActive' => false,
146
    ];
147
148
    $this->assertEquals([$english, $french, $spanish, $brazil], $result['data']['frontpage']['languageSwitchLinks']);
149
  }
150
151
}
152