Chris@0: drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'translate interface']); Chris@0: $this->drupalLogin($admin_user); Chris@0: $this->addLanguage('de'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests interface translation update using cron. Chris@0: */ Chris@0: public function testUpdateCron() { Chris@0: // Set a flag to let the locale_test module replace the project data with a Chris@0: // set of test projects. Chris@0: \Drupal::state()->set('locale.test_projects_alter', TRUE); Chris@0: Chris@0: // Setup local and remote translations files. Chris@0: $this->setTranslationFiles(); Chris@0: $this->config('locale.settings')->set('translation.default_filename', '%project-%version.%language._po')->save(); Chris@0: Chris@0: // Update translations using batch to ensure a clean test starting point. Chris@0: $this->drupalGet('admin/reports/translations/check'); Chris@0: $this->drupalPostForm('admin/reports/translations', [], t('Update translations')); Chris@0: Chris@0: // Store translation status for comparison. Chris@0: $initial_history = locale_translation_get_file_history(); Chris@0: Chris@0: // Prepare for test: Simulate new translations being available. Chris@0: // Change the last updated timestamp of a translation file. Chris@0: $contrib_module_two_uri = 'public://local/contrib_module_two-8.x-2.0-beta4.de._po'; Chris@14: touch(\Drupal::service('file_system')->realpath($contrib_module_two_uri), REQUEST_TIME); Chris@0: Chris@0: // Prepare for test: Simulate that the file has not been checked for a long Chris@0: // time. Set the last_check timestamp to zero. Chris@18: $query = Database::getConnection()->update('locale_file'); Chris@0: $query->fields(['last_checked' => 0]); Chris@0: $query->condition('project', 'contrib_module_two'); Chris@0: $query->condition('langcode', 'de'); Chris@0: $query->execute(); Chris@0: Chris@0: // Test: Disable cron update and verify that no tasks are added to the Chris@0: // queue. Chris@0: $edit = [ Chris@0: 'update_interval_days' => 0, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration')); Chris@0: Chris@0: // Execute locale cron tasks to add tasks to the queue. Chris@0: locale_cron(); Chris@0: Chris@0: // Check whether no tasks are added to the queue. Chris@0: $queue = \Drupal::queue('locale_translation', TRUE); Chris@0: $this->assertEqual($queue->numberOfItems(), 0, 'Queue is empty'); Chris@0: Chris@0: // Test: Enable cron update and check if update tasks are added to the Chris@0: // queue. Chris@0: // Set cron update to Weekly. Chris@0: $edit = [ Chris@0: 'update_interval_days' => 7, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration')); Chris@0: Chris@0: // Execute locale cron tasks to add tasks to the queue. Chris@0: locale_cron(); Chris@0: Chris@0: // Check whether tasks are added to the queue. Chris@0: $queue = \Drupal::queue('locale_translation', TRUE); Chris@16: $this->assertEqual($queue->numberOfItems(), 2, 'Queue holds tasks for one project.'); Chris@0: $item = $queue->claimItem(); Chris@0: $queue->releaseItem($item); Chris@0: $this->assertEqual($item->data[1][0], 'contrib_module_two', 'Queue holds tasks for contrib module one.'); Chris@0: Chris@0: // Test: Run cron for a second time and check if tasks are not added to Chris@0: // the queue twice. Chris@0: locale_cron(); Chris@0: Chris@0: // Check whether no more tasks are added to the queue. Chris@0: $queue = \Drupal::queue('locale_translation', TRUE); Chris@16: $this->assertEqual($queue->numberOfItems(), 2, 'Queue holds tasks for one project.'); Chris@0: Chris@0: // Ensure last checked is updated to a greater time than the initial value. Chris@0: sleep(1); Chris@0: // Test: Execute cron and check if tasks are executed correctly. Chris@0: // Run cron to process the tasks in the queue. Chris@0: $this->cronRun(); Chris@0: Chris@0: drupal_static_reset('locale_translation_get_file_history'); Chris@0: $history = locale_translation_get_file_history(); Chris@0: $initial = $initial_history['contrib_module_two']['de']; Chris@0: $current = $history['contrib_module_two']['de']; Chris@0: $this->assertTrue($current->timestamp > $initial->timestamp, 'Timestamp is updated'); Chris@0: $this->assertTrue($current->last_checked > $initial->last_checked, 'Last checked is updated'); Chris@0: } Chris@0: Chris@0: }