L10nUpdateCronTest.test 4.3 KB

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