L10nUpdateCronTest.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @file
  4. * Contains L10nUpdateCronTest.
  5. */
  6. /**
  7. * Tests for translation update using cron.
  8. */
  9. class L10nUpdateCronTest extends L10nUpdateTestBase {
  10. protected $batch_output = array();
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public static function getInfo() {
  15. return array(
  16. 'name' => 'Update translations using cron',
  17. 'description' => 'Tests for using cron to update project interface translations.',
  18. 'group' => 'Localization Update',
  19. );
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function setUp() {
  25. parent::setUp();
  26. $admin_user = $this->drupalCreateUser(array('administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'translate interface'));
  27. $this->drupalLogin($admin_user);
  28. $this->addLanguage('de');
  29. }
  30. /**
  31. * Tests interface translation update using cron.
  32. */
  33. public function testUpdateCron() {
  34. // Set a flag to let the l10n_update_test module replace the project data
  35. // with a set of test projects.
  36. variable_set('l10n_update_test_projects_alter', TRUE);
  37. // Setup local and remote translations files.
  38. $this->setTranslationFiles();
  39. variable_set('l10n_update_default_filename', '%project-%release.%language._po');
  40. // Update translations using batch to ensure a clean test starting point.
  41. $this->drupalGet('admin/config/regional/translate/check');
  42. $this->drupalPost('admin/config/regional/translate/update', array(), t('Update translations'));
  43. // Store translation status for comparison.
  44. $initial_history = l10n_update_get_file_history();
  45. // Prepare for test: Simulate new translations being available.
  46. // Change the last updated timestamp of a translation file.
  47. $contrib_module_two_uri = 'public://local/contrib_module_two-7.x-2.0-beta4.de._po';
  48. touch(drupal_realpath($contrib_module_two_uri), REQUEST_TIME);
  49. // Prepare for test: Simulate that the file has not been checked for a long
  50. // time. Set the last_check timestamp to zero.
  51. $query = db_update('l10n_update_file');
  52. $query->fields(array('last_checked' => 0));
  53. $query->condition('project', 'contrib_module_two');
  54. $query->condition('language', 'de');
  55. $query->execute();
  56. // Test: Disable cron update and verify that no tasks are added to the
  57. // queue.
  58. $edit = array(
  59. 'l10n_update_check_frequency' => '0',
  60. );
  61. $this->drupalPost('admin/config/regional/language/update', $edit, t('Save configuration'));
  62. // Execute l10n_update cron taks to add tasks to the queue.
  63. l10n_update_cron();
  64. // Check whether no tasks are added to the queue.
  65. $queue = DrupalQueue::get('l10n_update', TRUE);
  66. $this->assertEqual($queue->numberOfItems(), 0, 'Queue is empty');
  67. // Test: Enable cron update and check if update tasks are added to the
  68. // queue.
  69. // Set cron update to Weekly.
  70. $edit = array(
  71. 'l10n_update_check_frequency' => '7',
  72. );
  73. $this->drupalPost('admin/config/regional/language/update', $edit, t('Save configuration'));
  74. // Execute l10n_update cron task to add tasks to the queue.
  75. l10n_update_cron();
  76. // Check whether tasks are added to the queue.
  77. $queue = DrupalQueue::get('l10n_update', TRUE);
  78. $this->assertEqual($queue->numberOfItems(), 3, 'Queue holds tasks for one project.');
  79. $item = $queue->claimItem();
  80. $queue->releaseItem($item);
  81. $this->assertEqual($item->data[1][0], 'contrib_module_two', 'Queue holds tasks for contrib module one.');
  82. // Test: Run cron for a second time and check if tasks are not added to
  83. // the queue twice.
  84. l10n_update_cron();
  85. // Check whether no more tasks are added to the queue.
  86. $queue = DrupalQueue::get('l10n_update', TRUE);
  87. $this->assertEqual($queue->numberOfItems(), 3, 'Queue holds tasks for one project.');
  88. // Ensure last checked is updated to a greater time than the initial value.
  89. sleep(1);
  90. // Test: Execute cron and check if tasks are executed correctly.
  91. // Run cron to process the tasks in the queue.
  92. $this->drupalGet('admin/reports/status/run-cron');
  93. drupal_static_reset('l10n_update_get_file_history');
  94. $history = l10n_update_get_file_history();
  95. $initial = $initial_history['contrib_module_two']['de'];
  96. $current = $history['contrib_module_two']['de'];
  97. $this->assertTrue($current->timestamp > $initial->timestamp, 'Timestamp is updated');
  98. $this->assertTrue($current->last_checked > $initial->last_checked, 'Last checked is updated');
  99. }
  100. }