ConfigTranslationOverviewTest.php 5.6 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\config_translation\Functional;
4

5
use Drupal\Component\Utility\Html;
6
use Drupal\language\Entity\ConfigurableLanguage;
7
use Drupal\Tests\BrowserTestBase;
8
9

/**
10
11
12
 * Translate settings and entities to various languages.
 *
 * @group config_translation
13
 */
14
class ConfigTranslationOverviewTest extends BrowserTestBase {
15
16
17
18
19
20

  /**
   * Modules to enable.
   *
   * @var array
   */
21
  public static $modules = [
22
    'block',
23
24
25
26
27
28
29
30
31
    'config_test',
    'config_translation',
    'config_translation_test',
    'contact',
    'contextual',
    'entity_test_operation',
    'views',
    'views_ui',
  ];
32
33
34
35
36
37

  /**
   * Languages to enable.
   *
   * @var array
   */
38
  protected $langcodes = ['fr', 'ta'];
39
40
41
42
43
44
45
46

  /**
   * String translation storage object.
   *
   * @var \Drupal\locale\StringStorageInterface
   */
  protected $localeStorage;

47
  protected function setUp() {
48
    parent::setUp();
49
    $permissions = [
50
51
52
53
54
55
56
      'translate configuration',
      'administer languages',
      'administer site configuration',
      'administer contact forms',
      'access site-wide contact form',
      'access contextual links',
      'administer views',
57
    ];
58
    // Create and log in user.
59
60
61
62
    $this->drupalLogin($this->drupalCreateUser($permissions));

    // Add languages.
    foreach ($this->langcodes as $langcode) {
63
      ConfigurableLanguage::createFromLangcode($langcode)->save();
64
65
    }
    $this->localeStorage = $this->container->get('locale.storage');
66
    $this->drupalPlaceBlock('local_tasks_block');
67
    $this->drupalPlaceBlock('page_title_block');
68
69
70
71
72
73
74
75
76
  }

  /**
   * Tests the config translation mapper page.
   */
  public function testMapperListPage() {
    $this->drupalGet('admin/config/regional/config-translation');
    $this->assertLinkByHref('admin/config/regional/config-translation/config_test');
    $this->assertLinkByHref('admin/config/people/accounts/translate');
77
78
79
    // Make sure there is only a single operation for each dropbutton, either
    // 'List' or 'Translate'.
    foreach ($this->cssSelect('ul.dropbutton') as $i => $dropbutton) {
80
      $this->assertIdentical(1, count($dropbutton->findAll('xpath', 'li')));
81
      $this->assertTrue(($dropbutton->getText() === 'Translate') || ($dropbutton->getText() === 'List'));
82
    }
83

84
    $labels = [
85
86
87
      '&$nxd~i0',
      'some "label" with quotes',
      $this->randomString(),
88
    ];
89

90
    $storage = \Drupal::entityTypeManager()->getStorage('config_test');
91
    foreach ($labels as $label) {
92
      $test_entity = $storage->create([
93
        'id' => $this->randomMachineName(),
94
        'label' => $label,
95
      ]);
96
97
98
99
100
      $test_entity->save();

      $base_url = 'admin/structure/config_test/manage/' . $test_entity->id();
      $this->drupalGet('admin/config/regional/config-translation/config_test');
      $this->assertLinkByHref($base_url . '/translate');
101
      $this->assertEscaped($test_entity->label());
102

103
104
105
      // Make sure there is only a single 'Translate' operation for each
      // dropbutton.
      foreach ($this->cssSelect('ul.dropbutton') as $i => $dropbutton) {
106
        $this->assertIdentical(1, count($dropbutton->findAll('xpath', 'li')));
107
        $this->assertIdentical('Translate', $dropbutton->getText());
108
109
      }

110
      $entity_type = \Drupal::entityManager()->getDefinition($test_entity->getEntityTypeId());
111
112
      $this->drupalGet($base_url . '/translate');

113
114
      $title = $test_entity->label() . ' ' . $entity_type->getLowercaseLabel();
      $title = 'Translations for <em class="placeholder">' . Html::escape($title) . '</em>';
115
116
117
118
      $this->assertRaw($title);
      $this->assertRaw('<th>' . t('Language') . '</th>');

      $this->drupalGet($base_url);
119
      $this->assertLink(t('Translate @title', ['@title' => $entity_type->getLowercaseLabel()]));
120
121
122
123
124
125
126
127
128
    }
  }

  /**
   * Tests availability of hidden entities in the translation overview.
   */
  public function testHiddenEntities() {
    // Hidden languages are only available to translate through the
    // configuration translation listings.
129
    $this->drupalGet('admin/config/regional/config-translation/configurable_language');
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    $this->assertText('Not applicable');
    $this->assertLinkByHref('admin/config/regional/language/edit/zxx/translate');
    $this->assertText('Not specified');
    $this->assertLinkByHref('admin/config/regional/language/edit/und/translate');

    // Hidden date formats are only available to translate through the
    // configuration translation listings. Test a couple of them.
    $this->drupalGet('admin/config/regional/config-translation/date_format');
    $this->assertText('HTML Date');
    $this->assertLinkByHref('admin/config/regional/date-time/formats/manage/html_date/translate');
    $this->assertText('HTML Year');
    $this->assertLinkByHref('admin/config/regional/date-time/formats/manage/html_year/translate');
  }

144
145
146
147
148
149
150
  /**
   * Tests that overrides do not affect listing screens.
   */
  public function testListingPageWithOverrides() {
    $original_label = 'Default';
    $overridden_label = 'Overridden label';

151
152
    $config_test_storage = $this->container->get('entity.manager')->getStorage('config_test');

153
    // Set up an override.
154
    $settings['config']['config_test.dynamic.dotted.default']['label'] = (object) [
155
156
      'value' => $overridden_label,
      'required' => TRUE,
157
    ];
158
159
160
    $this->writeSettings($settings);

    // Test that the overridden label is loaded with the entity.
161
    $this->assertEqual($config_test_storage->load('dotted.default')->label(), $overridden_label);
162
163
164
165
166
167
168

    // Test that the original label on the listing page is intact.
    $this->drupalGet('admin/config/regional/config-translation/config_test');
    $this->assertText($original_label);
    $this->assertNoText($overridden_label);
  }

169
}