LocaleConfigSubscriberTest.php 16.4 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\locale\Kernel;
4 5

use Drupal\language\Entity\ConfigurableLanguage;
6
use Drupal\locale\Locale;
7 8
use Drupal\locale\StringInterface;
use Drupal\locale\TranslationString;
9
use Drupal\KernelTests\KernelTestBase;
10 11 12 13 14 15 16 17 18 19 20

/**
 * Tests that shipped configuration translations are updated correctly.
 *
 * @group locale
 */
class LocaleConfigSubscriberTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
21
  public static $modules = ['language', 'locale', 'system', 'locale_test'];
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

  /**
   * The configurable language manager used in this test.
   *
   * @var \Drupal\language\ConfigurableLanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The configuration factory used in this test.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The string storage used in this test.
   *
40
   * @var \Drupal\locale\StringStorageInterface
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
   */
  protected $stringStorage;

  /**
   * The locale configuration manager used in this test.
   *
   * @var \Drupal\locale\LocaleConfigManager
   */
  protected $localeConfigManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

57
    $this->setUpDefaultLanguage();
58 59 60

    $this->installSchema('locale', ['locales_source', 'locales_target', 'locales_location']);

61 62
    $this->setupLanguages();

63
    $this->installConfig(['locale_test']);
64 65
    // Simulate this hook invoked which would happen if in a non-kernel test
    // or normal environment.
66 67 68 69
    // @see locale_modules_installed()
    // @see locale_system_update()
    locale_system_set_config_langcodes();
    $langcodes = array_keys(\Drupal::languageManager()->getLanguages());
70
    $names = Locale::config()->getComponentNames();
71
    Locale::config()->updateConfigTranslations($names, $langcodes);
72 73 74 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 101

    $this->configFactory = $this->container->get('config.factory');
    $this->stringStorage = $this->container->get('locale.storage');
    $this->localeConfigManager = $this->container->get('locale.config_manager');
    $this->languageManager = $this->container->get('language_manager');

    $this->setUpLocale();
  }

  /**
   * Sets up default language for this test.
   */
  protected function setUpDefaultLanguage() {
    // Keep the default English.
  }

  /**
   * Sets up languages needed for this test.
   */
  protected function setUpLanguages() {
    ConfigurableLanguage::createFromLangcode('de')->save();
  }

  /**
   * Sets up the locale storage strings to be in line with configuration.
   */
  protected function setUpLocale() {
    // Set up the locale database the same way we have in the config samples.
    $this->setUpNoTranslation('locale_test.no_translation', 'test', 'Test', 'de');
    $this->setUpTranslation('locale_test.translation', 'test', 'English test', 'German test', 'de');
102 103 104 105 106 107 108 109
  }

  /**
   * Tests creating translations of shipped configuration.
   */
  public function testCreateTranslation() {
    $config_name = 'locale_test.no_translation';

110 111
    $this->saveLanguageOverride($config_name, 'test', 'Test (German)', 'de');
    $this->assertTranslation($config_name, 'Test (German)', 'de');
112 113 114 115 116 117 118 119
  }

  /**
   * Tests importing community translations of shipped configuration.
   */
  public function testLocaleCreateTranslation() {
    $config_name = 'locale_test.no_translation';

120 121
    $this->saveLocaleTranslationData($config_name, 'test', 'Test', 'Test (German)', 'de');
    $this->assertTranslation($config_name, 'Test (German)', 'de', FALSE);
122 123 124 125 126 127 128 129
  }

  /**
   * Tests updating translations of shipped configuration.
   */
  public function testUpdateTranslation() {
    $config_name = 'locale_test.translation';

130 131
    $this->saveLanguageOverride($config_name, 'test', 'Updated German test', 'de');
    $this->assertTranslation($config_name, 'Updated German test', 'de');
132 133 134 135 136 137 138 139
  }

  /**
   * Tests updating community translations of shipped configuration.
   */
  public function testLocaleUpdateTranslation() {
    $config_name = 'locale_test.translation';

140 141
    $this->saveLocaleTranslationData($config_name, 'test', 'English test', 'Updated German test', 'de');
    $this->assertTranslation($config_name, 'Updated German test', 'de', FALSE);
142 143 144 145 146 147 148 149
  }

  /**
   * Tests deleting translations of shipped configuration.
   */
  public function testDeleteTranslation() {
    $config_name = 'locale_test.translation';

150
    $this->deleteLanguageOverride($config_name, 'test', 'English test', 'de');
151 152 153
    // Instead of deleting the translation, we need to keep a translation with
    // the source value and mark it as customized to prevent the deletion being
    // reverted by importing community translations.
154
    $this->assertTranslation($config_name, 'English test', 'de');
155 156 157 158 159 160 161 162
  }

  /**
   * Tests deleting community translations of shipped configuration.
   */
  public function testLocaleDeleteTranslation() {
    $config_name = 'locale_test.translation';

163 164
    $this->deleteLocaleTranslationData($config_name, 'test', 'English test', 'de');
    $this->assertNoTranslation($config_name, 'de');
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  }

  /**
   * Sets up a configuration string without a translation.
   *
   * The actual configuration is already available by installing locale_test
   * module, as it is done in LocaleConfigSubscriberTest::setUp(). This sets up
   * the necessary source string and verifies that everything is as expected to
   * avoid false positives.
   *
   * @param string $config_name
   *   The configuration name.
   * @param string $key
   *   The configuration key.
   * @param string $source
   *   The source string.
181 182
   * @param string $langcode
   *   The language code.
183
   */
184
  protected function setUpNoTranslation($config_name, $key, $source, $langcode) {
185
    $this->localeConfigManager->updateConfigTranslations([$config_name], [$langcode]);
186 187
    $this->assertNoConfigOverride($config_name, $key, $source, $langcode);
    $this->assertNoTranslation($config_name, $langcode);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  }

  /**
   * Sets up a configuration string with a translation.
   *
   * The actual configuration is already available by installing locale_test
   * module, as it is done in LocaleConfigSubscriberTest::setUp(). This sets up
   * the necessary source and translation strings and verifies that everything
   * is as expected to avoid false positives.
   *
   * @param string $config_name
   *   The configuration name.
   * @param string $key
   *   The configuration key.
   * @param string $source
   *   The source string.
   * @param string $translation
   *   The translation string.
206 207 208 209
   * @param string $langcode
   *   The language code.
   * @param bool $is_active
   *   Whether the update will affect the active configuration.
210
   */
211
  protected function setUpTranslation($config_name, $key, $source, $translation, $langcode, $is_active = FALSE) {
212 213
    // Create source and translation strings for the configuration value and add
    // the configuration name as a location. This would be performed by
214 215 216 217 218 219 220 221 222 223
    // locale_translate_batch_import() invoking
    // LocaleConfigManager::updateConfigTranslations() normally.
    $this->localeConfigManager->reset();
    $this->localeConfigManager
      ->getStringTranslation($config_name, $langcode, $source, '')
      ->setString($translation)
      ->setCustomized(FALSE)
      ->save();
    $this->configFactory->reset($config_name);
    $this->localeConfigManager->reset();
224
    $this->localeConfigManager->updateConfigTranslations([$config_name], [$langcode]);
225 226 227 228 229 230 231 232

    if ($is_active) {
      $this->assertActiveConfig($config_name, $key, $translation, $langcode);
    }
    else {
      $this->assertConfigOverride($config_name, $key, $translation, $langcode);
    }
    $this->assertTranslation($config_name, $translation, $langcode, FALSE);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  }

  /**
   * Saves a language override.
   *
   * This will invoke LocaleConfigSubscriber through the event dispatcher. To
   * make sure the configuration was persisted correctly, the configuration
   * value is checked. Because LocaleConfigSubscriber temporarily disables the
   * override state of the configuration factory we check that the correct value
   * is restored afterwards.
   *
   * @param string $config_name
   *   The configuration name.
   * @param string $key
   *   The configuration key.
   * @param string $value
   *   The configuration value to save.
250 251
   * @param string $langcode
   *   The language code.
252
   */
253
  protected function saveLanguageOverride($config_name, $key, $value, $langcode) {
254
    $translation_override = $this->languageManager
255
      ->getLanguageConfigOverride($langcode, $config_name);
256 257 258 259 260
    $translation_override
      ->set($key, $value)
      ->save();
    $this->configFactory->reset($config_name);

261
    $this->assertConfigOverride($config_name, $key, $value, $langcode);
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  }

  /**
   * Saves translation data from locale module.
   *
   * This will invoke LocaleConfigSubscriber through the event dispatcher. To
   * make sure the configuration was persisted correctly, the configuration
   * value is checked. Because LocaleConfigSubscriber temporarily disables the
   * override state of the configuration factory we check that the correct value
   * is restored afterwards.
   *
   * @param string $config_name
   *   The configuration name.
   * @param string $key
   *   The configuration key.
277 278 279 280 281 282 283 284
   * @param string $source
   *   The source string.
   * @param string $translation
   *   The translation string to save.
   * @param string $langcode
   *   The language code.
   * @param bool $is_active
   *   Whether the update will affect the active configuration.
285
   */
286 287
  protected function saveLocaleTranslationData($config_name, $key, $source, $translation, $langcode, $is_active = FALSE) {
    $this->localeConfigManager->reset();
288
    $this->localeConfigManager
289 290 291 292
      ->getStringTranslation($config_name, $langcode, $source, '')
      ->setString($translation)
      ->save();
    $this->localeConfigManager->reset();
293
    $this->localeConfigManager->updateConfigTranslations([$config_name], [$langcode]);
294 295
    $this->configFactory->reset($config_name);

296 297 298 299 300 301
    if ($is_active) {
      $this->assertActiveConfig($config_name, $key, $translation, $langcode);
    }
    else {
      $this->assertConfigOverride($config_name, $key, $translation, $langcode);
    }
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  }

  /**
   * Deletes a language override.
   *
   * This will invoke LocaleConfigSubscriber through the event dispatcher. To
   * make sure the configuration was persisted correctly, the configuration
   * value is checked. Because LocaleConfigSubscriber temporarily disables the
   * override state of the configuration factory we check that the correct value
   * is restored afterwards.
   *
   * @param string $config_name
   *   The configuration name.
   * @param string $key
   *   The configuration key.
   * @param string $source_value
   *   The source configuration value to verify the correct value is returned
   *   from the configuration factory after the deletion.
320 321
   * @param string $langcode
   *   The language code.
322
   */
323
  protected function deleteLanguageOverride($config_name, $key, $source_value, $langcode) {
324
    $translation_override = $this->languageManager
325
      ->getLanguageConfigOverride($langcode, $config_name);
326 327 328 329 330
    $translation_override
      ->clear($key)
      ->save();
    $this->configFactory->reset($config_name);

331
    $this->assertNoConfigOverride($config_name, $key, $source_value, $langcode);
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  }

  /**
   * Deletes translation data from locale module.
   *
   * This will invoke LocaleConfigSubscriber through the event dispatcher. To
   * make sure the configuration was persisted correctly, the configuration
   * value is checked. Because LocaleConfigSubscriber temporarily disables the
   * override state of the configuration factory we check that the correct value
   * is restored afterwards.
   *
   * @param string $config_name
   *   The configuration name.
   * @param string $key
   *   The configuration key.
   * @param string $source_value
   *   The source configuration value to verify the correct value is returned
   *   from the configuration factory after the deletion.
350 351
   * @param string $langcode
   *   The language code.
352
   */
353 354 355 356 357
  protected function deleteLocaleTranslationData($config_name, $key, $source_value, $langcode) {
    $this->localeConfigManager
      ->getStringTranslation($config_name, $langcode, $source_value, '')
      ->delete();
    $this->localeConfigManager->reset();
358
    $this->localeConfigManager->updateConfigTranslations([$config_name], [$langcode]);
359 360
    $this->configFactory->reset($config_name);

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    $this->assertNoConfigOverride($config_name, $key, $source_value, $langcode);
  }

  /**
   * Ensures configuration override is not present anymore.
   *
   * @param string $config_name
   *   The configuration name.
   * @param string $langcode
   *   The language code.
   *
   * @return bool
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
  protected function assertNoConfigOverride($config_name, $langcode) {
    $config_langcode = $this->configFactory->getEditable($config_name)->get('langcode');
    $override = $this->languageManager->getLanguageConfigOverride($langcode, $config_name);
    return $this->assertNotEqual($config_langcode, $langcode) && $this->assertEqual($override->isNew(), TRUE);
  }

  /**
   * Ensures configuration was saved correctly.
   *
   * @param string $config_name
   *   The configuration name.
   * @param string $key
   *   The configuration key.
   * @param string $value
   *   The configuration value.
   * @param string $langcode
   *   The language code.
   *
   * @return bool
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
  protected function assertConfigOverride($config_name, $key, $value, $langcode) {
    $config_langcode = $this->configFactory->getEditable($config_name)->get('langcode');
    $override = $this->languageManager->getLanguageConfigOverride($langcode, $config_name);
    return $this->assertNotEqual($config_langcode, $langcode) && $this->assertEqual($override->get($key), $value);
400 401 402 403 404
  }

  /**
   * Ensures configuration was saved correctly.
   *
405
   * @param string $config_name
406
   *   The configuration name.
407
   * @param string $key
408
   *   The configuration key.
409
   * @param string $value
410
   *   The configuration value.
411 412
   * @param string $langcode
   *   The language code.
413 414 415 416
   *
   * @return bool
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
417 418 419 420 421
  protected function assertActiveConfig($config_name, $key, $value, $langcode) {
    $config = $this->configFactory->getEditable($config_name);
    return
      $this->assertEqual($config->get('langcode'), $langcode) &&
      $this->assertIdentical($config->get($key), $value);
422 423 424 425 426 427 428
  }

  /**
   * Ensures no translation exists.
   *
   * @param string $config_name
   *   The configuration name.
429 430
   * @param string $langcode
   *   The language code.
431 432 433 434
   *
   * @return bool
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
435
  protected function assertNoTranslation($config_name, $langcode) {
436 437 438
    $strings = $this->stringStorage->getTranslations([
      'type' => 'configuration',
      'name' => $config_name,
439
      'language' => $langcode,
440 441 442 443 444 445 446 447 448 449 450 451
      'translated' => TRUE,
    ]);
    return $this->assertIdentical([], $strings);
  }

  /**
   * Ensures a translation exists and is marked as customized.
   *
   * @param string $config_name
   *   The configuration name.
   * @param string $translation
   *   The translation.
452 453
   * @param string $langcode
   *   The language code.
454 455 456 457 458 459 460
   * @param bool $customized
   *   Whether or not the string should be asserted to be customized or not
   *   customized.
   *
   * @return bool
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
461
  protected function assertTranslation($config_name, $translation, $langcode, $customized = TRUE) {
462 463 464 465
    // Make sure a string exists.
    $strings = $this->stringStorage->getTranslations([
      'type' => 'configuration',
      'name' => $config_name,
466
      'language' => $langcode,
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
      'translated' => TRUE,
    ]);
    $pass = $this->assertIdentical(1, count($strings));
    $string = reset($strings);
    if ($this->assertTrue($string instanceof StringInterface)) {
      /** @var \Drupal\locale\StringInterface $string */
      $pass = $pass && $this->assertIdentical($translation, $string->getString());
      $pass = $pass && $this->assertTrue($string->isTranslation());
      if ($this->assertTrue($string instanceof TranslationString)) {
        /** @var \Drupal\locale\TranslationString $string */
        // Make sure the string is marked as customized so that it does not get
        // overridden when the string translations are updated.
        return $pass && $this->assertEqual($customized, $string->customized);
      }
    }
    return FALSE;
  }

}