PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/tine20/Scheduler/SchedulerTest.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 393 lines | 225 code | 75 blank | 93 comment | 1 complexity | 42f3c8019e85d095356ea2e2ee0c869e MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0 - http://www.tine20.org
  4. *
  5. * @package Scheduler
  6. * @license http://www.gnu.org/licenses/agpl.html
  7. * @copyright Copyright (c) 2008-2011 Metaways Infosystems GmbH (http://www.metaways.de)
  8. * @author Goekmen Ciyiltepe <g.ciyiltepe@metaways.de>
  9. */
  10. /**
  11. * Test helper
  12. */
  13. require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  14. /**
  15. * Test class for Scheduler Test
  16. */
  17. class Scheduler_SchedulerTest extends PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * Sets up unit tests.
  21. */
  22. public function setUp()
  23. {
  24. }
  25. /**
  26. * tears down the fixture
  27. */
  28. public function tearDown()
  29. {
  30. $scheduler = Tinebase_Core::getScheduler();
  31. $db = $scheduler->getBackend()->getDbAdapter();
  32. $db->delete(SQL_TABLE_PREFIX.'scheduler');
  33. // add initial tasks again
  34. $init = new Tinebase_Setup_Initialize();
  35. $init->initTinebaseScheduler();
  36. }
  37. /**
  38. * Tests if a task can be added.
  39. */
  40. public function testCanAddTask()
  41. {
  42. $scheduler = new Zend_Scheduler();
  43. $task = new Zend_Scheduler_Task();
  44. $scheduler->addTask('test', $task);
  45. $this->assertTrue($scheduler->hasTask('test'), 'Task could not be added');
  46. }
  47. /**
  48. * Tests if more than one task can be added.
  49. */
  50. public function testCanAddTasks()
  51. {
  52. $scheduler = new Zend_Scheduler();
  53. $tasks = array(
  54. 'test1' => new Zend_Scheduler_Task(),
  55. 'test2' => new Zend_Scheduler_Task()
  56. );
  57. $scheduler->addTasks($tasks);
  58. $this->assertTrue($scheduler->hasTask('test1'), 'Tasks could not be added');
  59. $this->assertTrue($scheduler->hasTask('test2'), 'Second task could not be added');
  60. }
  61. /**
  62. * Tests if tasks can be added from a Zend_Config file.
  63. */
  64. public function testCanAddConfig()
  65. {
  66. $scheduler = new Zend_Scheduler();
  67. $filename = dirname(__FILE__) . '/tasks.ini';
  68. $scheduler->addConfig(new Zend_Config_Ini($filename, 'tasks'), 'tasks');
  69. $this->assertTrue($scheduler->hasTask('test1'), 'Tasks could not be added');
  70. $this->assertTrue($scheduler->hasTask('test2'), 'Second task could not be added');
  71. }
  72. /**
  73. * Tests if a task can be removed.
  74. */
  75. public function testCanRemoveTask()
  76. {
  77. $scheduler = new Zend_Scheduler();
  78. $task = new Zend_Scheduler_Task();
  79. $scheduler->addTask('test', $task);
  80. $scheduler->removeTask('test');
  81. $this->assertFalse($scheduler->hasTask('test'), 'Task could not be removed');
  82. }
  83. /**
  84. * Tests to ensure that the scheduler cannot be serialized.
  85. *
  86. * This test is performed because serializing the scheduler depends on the
  87. * serialization of the controller, which is impractical.
  88. */
  89. public function testCannotSerializeScheduler()
  90. {
  91. $scheduler = new Zend_Scheduler();
  92. try {
  93. serialize($scheduler);
  94. } catch (Zend_Scheduler_Exception $e) {
  95. return true;
  96. }
  97. $this->fail('Did not prevent serialization of scheduler');
  98. }
  99. /**
  100. * Tests if a task can interpret basic rules.
  101. */
  102. public function testCanInterpretBasicRules()
  103. {
  104. $task = new Zend_Scheduler_Task();
  105. $task->setTime(mktime(23, 59, 59, 12, 31, 2006));
  106. $task->setMonths('December');
  107. $this->assertTrue($task->isScheduled(), 'Month rule was not interpreted correctly');
  108. $task->setDays('31');
  109. $this->assertTrue($task->isScheduled(), 'Day rule was not interpreted correctly');
  110. $task->setDays('last');
  111. $this->assertTrue($task->isScheduled(), "Keyword 'last' was not interpreted correctly");
  112. $task->setWeekdays('Sunday');
  113. $this->assertTrue($task->isScheduled(), 'Weekday rule was not interpreted correctly');
  114. $task->setHours('23');
  115. $this->assertTrue($task->isScheduled(), 'Hour rule was not interpreted correctly');
  116. $task->setMinutes('59');
  117. $this->assertTrue($task->isScheduled(), 'Minute rule was not interpreted correctly');
  118. }
  119. /**
  120. * Tests if a task can interpret the 'earliest run' rule.
  121. */
  122. public function testCanInterpretEarliestRunRule()
  123. {
  124. $task = new Zend_Scheduler_Task();
  125. $task->setTime(mktime(23, 59, 59, 12, 31, 2006));
  126. $task->setEarliestRun('2007-01-01T00:00:00');
  127. $this->assertFalse($task->isScheduled(), 'Earliest run rule was not interpreted correctly');
  128. $task->setEarliestRun('2006-01-01T00:00:00');
  129. $this->assertTrue($task->isScheduled(), 'Earliest run rule was not interpreted correctly');
  130. }
  131. /**
  132. * Tests if a task can interpret the 'latest run' rule.
  133. */
  134. public function testCanInterpretLatestRunRule()
  135. {
  136. $task = new Zend_Scheduler_Task();
  137. $task->setTime(mktime(23, 59, 59, 12, 31, 2006));
  138. $task->setLatestRun('2006-01-01T00:00:00');
  139. $this->assertFalse($task->isScheduled(), 'Latest run rule was not interpreted correctly');
  140. $task->setLatestRun('2007-01-01T00:00:00');
  141. $this->assertTrue($task->isScheduled(), 'Latest run rule was not interpreted correctly');
  142. }
  143. /**
  144. * Tests if a task can interpret rules with ranges, incuding those that
  145. * wrap from the maximum value to the minimum value.
  146. */
  147. public function testCanInterpretRangeRules()
  148. {
  149. $task = new Zend_Scheduler_Task();
  150. $task->setTime(mktime(23, 59, 59, 12, 31, 2006));
  151. $task->setMonths('October-December');
  152. $this->assertTrue($task->isScheduled(), 'Standard month range rule was not interpreted correctly');
  153. $task->setMonths('November-February');
  154. $this->assertTrue($task->isScheduled(), 'Wrap-around month range rule was not interpreted correctly');
  155. $task->setDays('25-31');
  156. $this->assertTrue($task->isScheduled(), 'Day range rule was not interpreted correctly');
  157. $task->setDays('25-3');
  158. $this->assertTrue($task->isScheduled(), 'Wrap-around day range rule was not interpreted correctly');
  159. $task->setDays('25-last');
  160. $this->assertTrue($task->isScheduled(), "Day range rule using keyword 'last' was not interpreted correctly");
  161. $task->setDays('last-3');
  162. $this->assertTrue($task->isScheduled(), "Wrap-around day range rule using keyword 'last' was not interpreted correctly");
  163. $task->setWeekdays('Sunday-Wednesday');
  164. $this->assertTrue($task->isScheduled(), 'Weekday range rule was not interpreted correctly');
  165. $task->setWeekdays('Friday-Wednesday');
  166. $this->assertTrue($task->isScheduled(), 'Wrap-around weekday range rule was not interpreted correctly');
  167. $task->setHours('18-23');
  168. $this->assertTrue($task->isScheduled(), 'Hour range rule was not interpreted correctly');
  169. $task->setHours('18-5');
  170. $this->assertTrue($task->isScheduled(), 'Wrap-around hour range rule was not interpreted correctly');
  171. $task->setMinutes('30-59');
  172. $this->assertTrue($task->isScheduled(), 'Minute range rule was not interpreted correctly');
  173. $task->setMinutes('50-10');
  174. $this->assertTrue($task->isScheduled(), 'Wrap-around minute range rule was not interpreted correctly');
  175. }
  176. /**
  177. * Tests if a task can interpret incremental step rules.
  178. */
  179. public function testCanInterpretStepRules()
  180. {
  181. $task = new Zend_Scheduler_Task();
  182. $task->setTime(mktime(23, 59, 59, 12, 31, 2006));
  183. $task->setDays('3/2');
  184. $this->assertTrue($task->isScheduled(), 'Day step rule was not interpreted correctly');
  185. $task->setHours('5/3');
  186. $this->assertTrue($task->isScheduled(), 'Hour step rule was not interpreted correctly');
  187. $task->setMinutes('5/9');
  188. $this->assertTrue($task->isScheduled(), 'Minute step rule was not interpreted correctly');
  189. }
  190. /**
  191. * Tests if a task gives an error when trying to use step rules in ways
  192. * which are not permitted.
  193. *
  194. * This test applies to months.
  195. */
  196. public function testCannotInterpretInvalidStepRules1()
  197. {
  198. $task = new Zend_Scheduler_Task();
  199. $task->setTime(mktime(23, 59, 59, 12, 31, 2006));
  200. $task->setMonths('January/3');
  201. try {
  202. $task->isScheduled();
  203. } catch (Zend_Scheduler_Exception $e) {
  204. return true;
  205. }
  206. $this->fail('Did not prevent invalid month step rule');
  207. }
  208. /**
  209. * Tests if a task gives an error when trying to use step rules in ways
  210. * which are not permitted.
  211. *
  212. * This test applies to days of the week ('weekdays').
  213. */
  214. public function testCannotInterpretInvalidStepRules2()
  215. {
  216. $task = new Zend_Scheduler_Task();
  217. $task->setTime(mktime(23, 59, 59, 12, 31, 2006));
  218. $task->setWeekdays('Monday/3');
  219. try {
  220. $task->isScheduled();
  221. } catch (Zend_Scheduler_Exception $e) {
  222. return true;
  223. }
  224. $this->fail('Did not prevent invalid weekday step rule');
  225. }
  226. /**
  227. * Tests if a task can be successfully dispatched.
  228. *
  229. * @see Zend_Controller_Front_Mock
  230. */
  231. public function testCanDispatchTask()
  232. {
  233. $controller = Zend_Controller_Front::getInstance();
  234. $controller->setControllerDirectory(dirname(__FILE__) . '/controllers');
  235. $controller->returnResponse(true);
  236. $controller->throwExceptions(true);
  237. $request = new Zend_Controller_Request_Http();
  238. $request->setControllerName('index');
  239. $request->setActionName('index');
  240. $scheduler = new Zend_Scheduler();
  241. $task = new Zend_Scheduler_Task();
  242. $task->setFrontController($controller);
  243. $task->setRequest($request);
  244. $scheduler->addTask('test', $task);
  245. $responses = $scheduler->run();
  246. $this->assertTrue(isset($responses['test']), 'Received empty response');
  247. }
  248. /**
  249. * Tests if a valid (i.e., included) backend can be loaded.
  250. */
  251. public function testCanLoadValidBackend()
  252. {
  253. $scheduler = new Zend_Scheduler();
  254. try {
  255. $scheduler->setBackend('File');
  256. } catch (Exception $e) {
  257. $this->fail('Denied the use of a valid backend');
  258. }
  259. }
  260. /**
  261. * Tests if the 'File' backend is functional.
  262. */
  263. public function testCanUseFileBackend()
  264. {
  265. $queue = dirname(__FILE__) . '/task.queue';
  266. $backend = new Zend_Scheduler_Backend_File(array('filename' => $queue));
  267. $this->_canUseBackend($backend);
  268. unset($queue);
  269. }
  270. /**
  271. * testActionQueueTriggeredByScheduler
  272. */
  273. public function testActionQueueTriggeredByScheduler()
  274. {
  275. if (! isset(Tinebase_Core::getConfig()->actionqueue)) {
  276. return;
  277. }
  278. $user = Tinebase_Core::getUser();
  279. $result1 = Tinebase_Container::getInstance()->getPersonalContainer($user, 'Addressbook', $user, '', TRUE);
  280. $scheduler = Tinebase_Core::getScheduler();
  281. Tinebase_Scheduler_Task::addQueueTask($scheduler);
  282. Tinebase_ActionQueue::getInstance()->queueAction('Addressbook.createPersonalFolder', $user);
  283. $scheduler->run();
  284. // check if user has 1 more personal folders now
  285. $result2 = Tinebase_Container::getInstance()->getPersonalContainer($user, 'Addressbook', $user, '', TRUE);
  286. $this->assertGreaterThan(count($result1), count($result2));
  287. $migration = $result2->getMigration($result1->getArrayOfIds());
  288. Tinebase_Container::getInstance()->delete($migration['toDeleteIds']);
  289. }
  290. /**
  291. * testCleanupCacheTriggeredByScheduler (this is only for code coverage)
  292. *
  293. * @see Tinebase_ControllerTest::testCleanupCache() for the real cache cleanup test
  294. */
  295. public function testCleanupCacheTriggeredByScheduler()
  296. {
  297. $scheduler = Tinebase_Core::getScheduler();
  298. Tinebase_Scheduler_Task::addCacheCleanupTask($scheduler);
  299. $scheduler->run();
  300. }
  301. /**
  302. * Tests any given backend.
  303. *
  304. * @param Zend_Scheduler $scheduler
  305. * @param Zend_Scheduler_Backend_Abstract $backend
  306. */
  307. protected function _canUseBackend(Zend_Scheduler_Backend_Abstract $backend)
  308. {
  309. $scheduler = new Zend_Scheduler($backend);
  310. try {
  311. $tasks = $backend->loadQueue();
  312. } catch (Zend_Scheduler_Exception $e) {
  313. $this->fail('Could not load task queue');
  314. }
  315. $taskCount = (count($tasks) > 0);
  316. $this->assertTrue($taskCount);
  317. }
  318. }