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