RequestTimeTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\WorkflowEngine\Tests\Check;
  22. use OCP\IL10N;
  23. class RequestTimeTest extends \Test\TestCase {
  24. /** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $timeFactory;
  26. /**
  27. * @return \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected function getL10NMock() {
  30. $l = $this->getMockBuilder(IL10N::class)
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $l->expects($this->any())
  34. ->method('t')
  35. ->willReturnCallback(function ($string, $args) {
  36. return sprintf($string, $args);
  37. });
  38. return $l;
  39. }
  40. protected function setUp() {
  41. parent::setUp();
  42. $this->timeFactory = $this->getMockBuilder('OCP\AppFramework\Utility\ITimeFactory')
  43. ->getMock();
  44. }
  45. public function dataExecuteCheck() {
  46. return [
  47. [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467870105, false], // 2016-07-07T07:41:45+02:00
  48. [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467873705, true], // 2016-07-07T08:41:45+02:00
  49. [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467902505, true], // 2016-07-07T16:41:45+02:00
  50. [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467906105, false], // 2016-07-07T17:41:45+02:00
  51. [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467870105, true], // 2016-07-07T07:41:45+02:00
  52. [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467873705, false], // 2016-07-07T08:41:45+02:00
  53. [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467902505, false], // 2016-07-07T16:41:45+02:00
  54. [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467906105, true], // 2016-07-07T17:41:45+02:00
  55. [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467843105, false], // 2016-07-07T07:41:45+09:30
  56. [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467846705, true], // 2016-07-07T08:41:45+09:30
  57. [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467875505, true], // 2016-07-07T16:41:45+09:30
  58. [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467879105, false], // 2016-07-07T17:41:45+09:30
  59. [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467843105, true], // 2016-07-07T07:41:45+09:30
  60. [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467846705, false], // 2016-07-07T08:41:45+09:30
  61. [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467875505, false], // 2016-07-07T16:41:45+09:30
  62. [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467879105, true], // 2016-07-07T17:41:45+09:30
  63. [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467916905, false], // 2016-07-07T07:41:45-11:00
  64. [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467920505, true], // 2016-07-07T08:41:45-11:00
  65. [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467949305, true], // 2016-07-07T16:41:45-11:00
  66. [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467952905, false], // 2016-07-07T17:41:45-11:00
  67. [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467916905, true], // 2016-07-07T07:41:45-11:00
  68. [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467920505, false], // 2016-07-07T08:41:45-11:00
  69. [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467949305, false], // 2016-07-07T16:41:45-11:00
  70. [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467952905, true], // 2016-07-07T17:41:45-11:00
  71. ];
  72. }
  73. /**
  74. * @dataProvider dataExecuteCheck
  75. * @param string $value
  76. * @param int $timestamp
  77. * @param bool $expected
  78. */
  79. public function testExecuteCheckIn($value, $timestamp, $expected) {
  80. $check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
  81. $this->timeFactory->expects($this->once())
  82. ->method('getTime')
  83. ->willReturn($timestamp);
  84. $this->assertEquals($expected, $check->executeCheck('in', $value));
  85. }
  86. /**
  87. * @dataProvider dataExecuteCheck
  88. * @param string $value
  89. * @param int $timestamp
  90. * @param bool $expected
  91. */
  92. public function testExecuteCheckNotIn($value, $timestamp, $expected) {
  93. $check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
  94. $this->timeFactory->expects($this->once())
  95. ->method('getTime')
  96. ->willReturn($timestamp);
  97. $this->assertEquals(!$expected, $check->executeCheck('!in', $value));
  98. }
  99. public function dataValidateCheck() {
  100. return [
  101. ['in', '["08:00 Europe/Berlin","17:00 Europe/Berlin"]'],
  102. ['!in', '["08:00 Europe/Berlin","17:00 America/North_Dakota/Beulah"]'],
  103. ['in', '["08:00 America/Port-au-Prince","17:00 America/Argentina/San_Luis"]'],
  104. ];
  105. }
  106. /**
  107. * @dataProvider dataValidateCheck
  108. * @param string $operator
  109. * @param string $value
  110. */
  111. public function testValidateCheck($operator, $value) {
  112. $check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
  113. $check->validateCheck($operator, $value);
  114. $this->addToAssertionCount(1);
  115. }
  116. public function dataValidateCheckInvalid() {
  117. return [
  118. ['!!in', '["08:00 Europe/Berlin","17:00 Europe/Berlin"]', 1, 'The given operator is invalid'],
  119. ['in', '["28:00 Europe/Berlin","17:00 Europe/Berlin"]', 2, 'The given time span is invalid'],
  120. ['in', '["08:00 Europe/Berlin","27:00 Europe/Berlin"]', 2, 'The given time span is invalid'],
  121. ['in', '["08:00 Europa/Berlin","17:00 Europe/Berlin"]', 3, 'The given start time is invalid'],
  122. ['in', '["08:00 Europe/Berlin","17:00 Europa/Berlin"]', 4, 'The given end time is invalid'],
  123. ['in', '["08:00 Europe/Bearlin","17:00 Europe/Berlin"]', 3, 'The given start time is invalid'],
  124. ['in', '["08:00 Europe/Berlin","17:00 Europe/Bearlin"]', 4, 'The given end time is invalid'],
  125. ];
  126. }
  127. /**
  128. * @dataProvider dataValidateCheckInvalid
  129. * @param string $operator
  130. * @param string $value
  131. * @param int $exceptionCode
  132. * @param string $exceptionMessage
  133. */
  134. public function testValidateCheckInvalid($operator, $value, $exceptionCode, $exceptionMessage) {
  135. $check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
  136. try {
  137. $check->validateCheck($operator, $value);
  138. } catch (\UnexpectedValueException $e) {
  139. $this->assertEquals($exceptionCode, $e->getCode());
  140. $this->assertEquals($exceptionMessage, $e->getMessage());
  141. }
  142. }
  143. }