annotate core/modules/locale/tests/src/Functional/LocaleUpdateCronTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\locale\Functional;
Chris@0 4
Chris@18 5 use Drupal\Core\Database\Database;
Chris@0 6 use Drupal\Tests\Traits\Core\CronRunTrait;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests for using cron to update project interface translations.
Chris@0 10 *
Chris@0 11 * @group locale
Chris@0 12 */
Chris@0 13 class LocaleUpdateCronTest extends LocaleUpdateBase {
Chris@0 14
Chris@0 15 use CronRunTrait;
Chris@0 16
Chris@0 17 protected $batchOutput = [];
Chris@0 18
Chris@0 19 /**
Chris@0 20 * {@inheritdoc}
Chris@0 21 */
Chris@0 22 protected function setUp() {
Chris@0 23 parent::setUp();
Chris@0 24 $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'translate interface']);
Chris@0 25 $this->drupalLogin($admin_user);
Chris@0 26 $this->addLanguage('de');
Chris@0 27 }
Chris@0 28
Chris@0 29 /**
Chris@0 30 * Tests interface translation update using cron.
Chris@0 31 */
Chris@0 32 public function testUpdateCron() {
Chris@0 33 // Set a flag to let the locale_test module replace the project data with a
Chris@0 34 // set of test projects.
Chris@0 35 \Drupal::state()->set('locale.test_projects_alter', TRUE);
Chris@0 36
Chris@0 37 // Setup local and remote translations files.
Chris@0 38 $this->setTranslationFiles();
Chris@0 39 $this->config('locale.settings')->set('translation.default_filename', '%project-%version.%language._po')->save();
Chris@0 40
Chris@0 41 // Update translations using batch to ensure a clean test starting point.
Chris@0 42 $this->drupalGet('admin/reports/translations/check');
Chris@0 43 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
Chris@0 44
Chris@0 45 // Store translation status for comparison.
Chris@0 46 $initial_history = locale_translation_get_file_history();
Chris@0 47
Chris@0 48 // Prepare for test: Simulate new translations being available.
Chris@0 49 // Change the last updated timestamp of a translation file.
Chris@0 50 $contrib_module_two_uri = 'public://local/contrib_module_two-8.x-2.0-beta4.de._po';
Chris@14 51 touch(\Drupal::service('file_system')->realpath($contrib_module_two_uri), REQUEST_TIME);
Chris@0 52
Chris@0 53 // Prepare for test: Simulate that the file has not been checked for a long
Chris@0 54 // time. Set the last_check timestamp to zero.
Chris@18 55 $query = Database::getConnection()->update('locale_file');
Chris@0 56 $query->fields(['last_checked' => 0]);
Chris@0 57 $query->condition('project', 'contrib_module_two');
Chris@0 58 $query->condition('langcode', 'de');
Chris@0 59 $query->execute();
Chris@0 60
Chris@0 61 // Test: Disable cron update and verify that no tasks are added to the
Chris@0 62 // queue.
Chris@0 63 $edit = [
Chris@0 64 'update_interval_days' => 0,
Chris@0 65 ];
Chris@0 66 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
Chris@0 67
Chris@0 68 // Execute locale cron tasks to add tasks to the queue.
Chris@0 69 locale_cron();
Chris@0 70
Chris@0 71 // Check whether no tasks are added to the queue.
Chris@0 72 $queue = \Drupal::queue('locale_translation', TRUE);
Chris@0 73 $this->assertEqual($queue->numberOfItems(), 0, 'Queue is empty');
Chris@0 74
Chris@0 75 // Test: Enable cron update and check if update tasks are added to the
Chris@0 76 // queue.
Chris@0 77 // Set cron update to Weekly.
Chris@0 78 $edit = [
Chris@0 79 'update_interval_days' => 7,
Chris@0 80 ];
Chris@0 81 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
Chris@0 82
Chris@0 83 // Execute locale cron tasks to add tasks to the queue.
Chris@0 84 locale_cron();
Chris@0 85
Chris@0 86 // Check whether tasks are added to the queue.
Chris@0 87 $queue = \Drupal::queue('locale_translation', TRUE);
Chris@16 88 $this->assertEqual($queue->numberOfItems(), 2, 'Queue holds tasks for one project.');
Chris@0 89 $item = $queue->claimItem();
Chris@0 90 $queue->releaseItem($item);
Chris@0 91 $this->assertEqual($item->data[1][0], 'contrib_module_two', 'Queue holds tasks for contrib module one.');
Chris@0 92
Chris@0 93 // Test: Run cron for a second time and check if tasks are not added to
Chris@0 94 // the queue twice.
Chris@0 95 locale_cron();
Chris@0 96
Chris@0 97 // Check whether no more tasks are added to the queue.
Chris@0 98 $queue = \Drupal::queue('locale_translation', TRUE);
Chris@16 99 $this->assertEqual($queue->numberOfItems(), 2, 'Queue holds tasks for one project.');
Chris@0 100
Chris@0 101 // Ensure last checked is updated to a greater time than the initial value.
Chris@0 102 sleep(1);
Chris@0 103 // Test: Execute cron and check if tasks are executed correctly.
Chris@0 104 // Run cron to process the tasks in the queue.
Chris@0 105 $this->cronRun();
Chris@0 106
Chris@0 107 drupal_static_reset('locale_translation_get_file_history');
Chris@0 108 $history = locale_translation_get_file_history();
Chris@0 109 $initial = $initial_history['contrib_module_two']['de'];
Chris@0 110 $current = $history['contrib_module_two']['de'];
Chris@0 111 $this->assertTrue($current->timestamp > $initial->timestamp, 'Timestamp is updated');
Chris@0 112 $this->assertTrue($current->last_checked > $initial->last_checked, 'Last checked is updated');
Chris@0 113 }
Chris@0 114
Chris@0 115 }