3 namespace Drupal\locale\Tests;
6 * Tests for using cron to update project interface translations.
10 class LocaleUpdateCronTest extends LocaleUpdateBase {
12 protected $batchOutput = [];
17 protected function setUp() {
19 $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'translate interface']);
20 $this->drupalLogin($admin_user);
21 $this->addLanguage('de');
25 * Tests interface translation update using cron.
27 public function testUpdateCron() {
28 // Set a flag to let the locale_test module replace the project data with a
29 // set of test projects.
30 \Drupal::state()->set('locale.test_projects_alter', TRUE);
32 // Setup local and remote translations files.
33 $this->setTranslationFiles();
34 $this->config('locale.settings')->set('translation.default_filename', '%project-%version.%language._po')->save();
36 // Update translations using batch to ensure a clean test starting point.
37 $this->drupalGet('admin/reports/translations/check');
38 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
40 // Store translation status for comparison.
41 $initial_history = locale_translation_get_file_history();
43 // Prepare for test: Simulate new translations being available.
44 // Change the last updated timestamp of a translation file.
45 $contrib_module_two_uri = 'public://local/contrib_module_two-8.x-2.0-beta4.de._po';
46 touch(drupal_realpath($contrib_module_two_uri), REQUEST_TIME);
48 // Prepare for test: Simulate that the file has not been checked for a long
49 // time. Set the last_check timestamp to zero.
50 $query = db_update('locale_file');
51 $query->fields(['last_checked' => 0]);
52 $query->condition('project', 'contrib_module_two');
53 $query->condition('langcode', 'de');
56 // Test: Disable cron update and verify that no tasks are added to the
59 'update_interval_days' => 0,
61 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
63 // Execute locale cron tasks to add tasks to the queue.
66 // Check whether no tasks are added to the queue.
67 $queue = \Drupal::queue('locale_translation', TRUE);
68 $this->assertEqual($queue->numberOfItems(), 0, 'Queue is empty');
70 // Test: Enable cron update and check if update tasks are added to the
72 // Set cron update to Weekly.
74 'update_interval_days' => 7,
76 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
78 // Execute locale cron tasks to add tasks to the queue.
81 // Check whether tasks are added to the queue.
82 $queue = \Drupal::queue('locale_translation', TRUE);
83 $this->assertEqual($queue->numberOfItems(), 3, 'Queue holds tasks for one project.');
84 $item = $queue->claimItem();
85 $queue->releaseItem($item);
86 $this->assertEqual($item->data[1][0], 'contrib_module_two', 'Queue holds tasks for contrib module one.');
88 // Test: Run cron for a second time and check if tasks are not added to
92 // Check whether no more tasks are added to the queue.
93 $queue = \Drupal::queue('locale_translation', TRUE);
94 $this->assertEqual($queue->numberOfItems(), 3, 'Queue holds tasks for one project.');
96 // Ensure last checked is updated to a greater time than the initial value.
98 // Test: Execute cron and check if tasks are executed correctly.
99 // Run cron to process the tasks in the queue.
102 drupal_static_reset('locale_translation_get_file_history');
103 $history = locale_translation_get_file_history();
104 $initial = $initial_history['contrib_module_two']['de'];
105 $current = $history['contrib_module_two']['de'];
106 $this->assertTrue($current->timestamp > $initial->timestamp, 'Timestamp is updated');
107 $this->assertTrue($current->last_checked > $initial->last_checked, 'Last checked is updated');