Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / locale / tests / src / Functional / LocaleUpdateCronTest.php
1 <?php
2
3 namespace Drupal\Tests\locale\Functional;
4
5 use Drupal\Tests\Traits\Core\CronRunTrait;
6
7 /**
8  * Tests for using cron to update project interface translations.
9  *
10  * @group locale
11  */
12 class LocaleUpdateCronTest extends LocaleUpdateBase {
13
14   use CronRunTrait;
15
16   protected $batchOutput = [];
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function setUp() {
22     parent::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');
26   }
27
28   /**
29    * Tests interface translation update using cron.
30    */
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);
35
36     // Setup local and remote translations files.
37     $this->setTranslationFiles();
38     $this->config('locale.settings')->set('translation.default_filename', '%project-%version.%language._po')->save();
39
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'));
43
44     // Store translation status for comparison.
45     $initial_history = locale_translation_get_file_history();
46
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);
51
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');
58     $query->execute();
59
60     // Test: Disable cron update and verify that no tasks are added to the
61     // queue.
62     $edit = [
63       'update_interval_days' => 0,
64     ];
65     $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
66
67     // Execute locale cron tasks to add tasks to the queue.
68     locale_cron();
69
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');
73
74     // Test: Enable cron update and check if update tasks are added to the
75     // queue.
76     // Set cron update to Weekly.
77     $edit = [
78       'update_interval_days' => 7,
79     ];
80     $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
81
82     // Execute locale cron tasks to add tasks to the queue.
83     locale_cron();
84
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.');
91
92     // Test: Run cron for a second time and check if tasks are not added to
93     // the queue twice.
94     locale_cron();
95
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.');
99
100     // Ensure last checked is updated to a greater time than the initial value.
101     sleep(1);
102     // Test: Execute cron and check if tasks are executed correctly.
103     // Run cron to process the tasks in the queue.
104     $this->cronRun();
105
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');
112   }
113
114 }