78a14ecac858c86131628abe3e0ce3dcb64d5f90
[yaffs-website] / web / core / modules / locale / src / Tests / LocaleUpdateCronTest.php
1 <?php
2
3 namespace Drupal\locale\Tests;
4
5 /**
6  * Tests for using cron to update project interface translations.
7  *
8  * @group locale
9  */
10 class LocaleUpdateCronTest extends LocaleUpdateBase {
11
12   protected $batchOutput = [];
13
14   /**
15    * {@inheritdoc}
16    */
17   protected function setUp() {
18     parent::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');
22   }
23
24   /**
25    * Tests interface translation update using cron.
26    */
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);
31
32     // Setup local and remote translations files.
33     $this->setTranslationFiles();
34     $this->config('locale.settings')->set('translation.default_filename', '%project-%version.%language._po')->save();
35
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'));
39
40     // Store translation status for comparison.
41     $initial_history = locale_translation_get_file_history();
42
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);
47
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');
54     $query->execute();
55
56     // Test: Disable cron update and verify that no tasks are added to the
57     // queue.
58     $edit = [
59       'update_interval_days' => 0,
60     ];
61     $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
62
63     // Execute locale cron tasks to add tasks to the queue.
64     locale_cron();
65
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');
69
70     // Test: Enable cron update and check if update tasks are added to the
71     // queue.
72     // Set cron update to Weekly.
73     $edit = [
74       'update_interval_days' => 7,
75     ];
76     $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
77
78     // Execute locale cron tasks to add tasks to the queue.
79     locale_cron();
80
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.');
87
88     // Test: Run cron for a second time and check if tasks are not added to
89     // the queue twice.
90     locale_cron();
91
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.');
95
96     // Ensure last checked is updated to a greater time than the initial value.
97     sleep(1);
98     // Test: Execute cron and check if tasks are executed correctly.
99     // Run cron to process the tasks in the queue.
100     $this->cronRun();
101
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');
108   }
109
110 }