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