You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
7.2 KiB

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