function LocaleFileHashTest::testModifiedFileProducesDifferentHash

Same name and namespace in other branches
  1. 11.x core/modules/locale/tests/src/Functional/LocaleFileHashTest.php \Drupal\Tests\locale\Functional\LocaleFileHashTest::testModifiedFileProducesDifferentHash()

Tests that modifying a file produces a different hash, detected by the UI.

File

core/modules/locale/tests/src/Functional/LocaleFileHashTest.php, line 60

Class

LocaleFileHashTest
Tests that file hash is used for local translation file change detection.

Namespace

Drupal\Tests\locale\Functional

Code

public function testModifiedFileProducesDifferentHash() : void {
  // Check for translation updates via the UI.
  $this->drupalGet('admin/reports/translations/check');
  // The translation status page should show no updates are available.
  $this->assertSession()
    ->addressEquals('admin/reports/translations');
  $this->assertSession()
    ->pageTextNotContains('Updates for:');
  $this->assertSession()
    ->pageTextContains('All translations up to date.');
  // Check the stored hash after import.
  $uri = 'translations://contrib_module_two-8.x-2.0-beta4.de._po';
  $initial_hash = hash_file(LocaleSource::LOCAL_FILE_HASH_ALGO, $uri);
  $this->assertHashes($initial_hash, $initial_hash, 'contrib_module_two', 'de');
  // Change the mtime of the file to show that only the hash is used for local
  // files.
  touch($uri, time() + 20000);
  // Run check again via the UI.
  $this->drupalGet('admin/reports/translations/check');
  // The translation status page should show no updates are available.
  $this->assertSession()
    ->addressEquals('admin/reports/translations');
  $this->assertSession()
    ->pageTextNotContains('Updates for:');
  $this->assertSession()
    ->pageTextContains('All translations up to date.');
  // Modify the content.
  file_put_contents($uri, "\nmsgid \"Extra\"\nmsgstr \"Zusatz\"\n", FILE_APPEND);
  // Set the mtime to the previous mtime to prove the hash is causing the
  // update.
  touch($uri, filemtime($uri));
  // Run check again via the UI.
  $this->drupalGet('admin/reports/translations/check');
  // The translation status page should show an update is available.
  $this->assertSession()
    ->addressEquals('admin/reports/translations');
  $this->assertSession()
    ->pageTextContains('Updates for: Contributed module two');
  // The hash computed by sourceCheckFile should differ because the file
  // content changed.
  $expected_hash = hash_file(LocaleSource::LOCAL_FILE_HASH_ALGO, $uri);
  $this->assertHashes($initial_hash, $expected_hash, 'contrib_module_two', 'de');
  $this->submitForm([], 'Update translations');
  // Check the stored hash after import.
  $this->assertHashes($expected_hash, $expected_hash, 'contrib_module_two', 'de');
  // Check for translation updates via the UI.
  $this->drupalGet('admin/reports/translations/check');
  // The translation status page should show no updates are available.
  $this->assertSession()
    ->addressEquals('admin/reports/translations');
  $this->assertSession()
    ->pageTextNotContains('Updates for:');
  $this->assertSession()
    ->pageTextContains('All translations up to date.');
  // Change the mtime of the file and empty the hash to prove that fallback
  // works.
  touch($uri, time() + 20000);
  $status = locale_translation_get_status([
    'contrib_module_two',
  ]);
  $status['contrib_module_two']['de']->hash = '';
  $status['contrib_module_two']['de']->files[LOCALE_TRANSLATION_LOCAL]->hash = '';
  \Drupal::keyValue('locale.translation_status')->set('contrib_module_two', $status['contrib_module_two']);
  // Test fallback to mtime if the hash is not available.
  $this->drupalGet('admin/reports/translations/check');
  // The translation status page should show no updates are available.
  $this->assertSession()
    ->addressEquals('admin/reports/translations');
  $this->assertSession()
    ->pageTextContains('Updates for: Contributed module two');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.