RequestTimeTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\WorkflowEngine\Tests\Check;
  7. use OCP\IL10N;
  8. class RequestTimeTest extends \Test\TestCase {
  9. /** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  10. protected $timeFactory;
  11. /**
  12. * @return \OCP\IL10N|\PHPUnit\Framework\MockObject\MockObject
  13. */
  14. protected function getL10NMock() {
  15. $l = $this->getMockBuilder(IL10N::class)
  16. ->disableOriginalConstructor()
  17. ->getMock();
  18. $l->expects($this->any())
  19. ->method('t')
  20. ->willReturnCallback(function ($string, $args) {
  21. return sprintf($string, $args);
  22. });
  23. return $l;
  24. }
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->timeFactory = $this->getMockBuilder('OCP\AppFramework\Utility\ITimeFactory')
  28. ->getMock();
  29. }
  30. public function dataExecuteCheck() {
  31. return [
  32. [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467870105, false], // 2016-07-07T07:41:45+02:00
  33. [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467873705, true], // 2016-07-07T08:41:45+02:00
  34. [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467902505, true], // 2016-07-07T16:41:45+02:00
  35. [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467906105, false], // 2016-07-07T17:41:45+02:00
  36. [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467870105, true], // 2016-07-07T07:41:45+02:00
  37. [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467873705, false], // 2016-07-07T08:41:45+02:00
  38. [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467902505, false], // 2016-07-07T16:41:45+02:00
  39. [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467906105, true], // 2016-07-07T17:41:45+02:00
  40. [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467843105, false], // 2016-07-07T07:41:45+09:30
  41. [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467846705, true], // 2016-07-07T08:41:45+09:30
  42. [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467875505, true], // 2016-07-07T16:41:45+09:30
  43. [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467879105, false], // 2016-07-07T17:41:45+09:30
  44. [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467843105, true], // 2016-07-07T07:41:45+09:30
  45. [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467846705, false], // 2016-07-07T08:41:45+09:30
  46. [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467875505, false], // 2016-07-07T16:41:45+09:30
  47. [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467879105, true], // 2016-07-07T17:41:45+09:30
  48. [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467916905, false], // 2016-07-07T07:41:45-11:00
  49. [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467920505, true], // 2016-07-07T08:41:45-11:00
  50. [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467949305, true], // 2016-07-07T16:41:45-11:00
  51. [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467952905, false], // 2016-07-07T17:41:45-11:00
  52. [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467916905, true], // 2016-07-07T07:41:45-11:00
  53. [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467920505, false], // 2016-07-07T08:41:45-11:00
  54. [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467949305, false], // 2016-07-07T16:41:45-11:00
  55. [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467952905, true], // 2016-07-07T17:41:45-11:00
  56. ];
  57. }
  58. /**
  59. * @dataProvider dataExecuteCheck
  60. * @param string $value
  61. * @param int $timestamp
  62. * @param bool $expected
  63. */
  64. public function testExecuteCheckIn($value, $timestamp, $expected) {
  65. $check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
  66. $this->timeFactory->expects($this->once())
  67. ->method('getTime')
  68. ->willReturn($timestamp);
  69. $this->assertEquals($expected, $check->executeCheck('in', $value));
  70. }
  71. /**
  72. * @dataProvider dataExecuteCheck
  73. * @param string $value
  74. * @param int $timestamp
  75. * @param bool $expected
  76. */
  77. public function testExecuteCheckNotIn($value, $timestamp, $expected) {
  78. $check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
  79. $this->timeFactory->expects($this->once())
  80. ->method('getTime')
  81. ->willReturn($timestamp);
  82. $this->assertEquals(!$expected, $check->executeCheck('!in', $value));
  83. }
  84. public function dataValidateCheck() {
  85. return [
  86. ['in', '["08:00 Europe/Berlin","17:00 Europe/Berlin"]'],
  87. ['!in', '["08:00 Europe/Berlin","17:00 America/North_Dakota/Beulah"]'],
  88. ['in', '["08:00 America/Port-au-Prince","17:00 America/Argentina/San_Luis"]'],
  89. ];
  90. }
  91. /**
  92. * @dataProvider dataValidateCheck
  93. * @param string $operator
  94. * @param string $value
  95. */
  96. public function testValidateCheck($operator, $value) {
  97. $check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
  98. $check->validateCheck($operator, $value);
  99. $this->addToAssertionCount(1);
  100. }
  101. public function dataValidateCheckInvalid() {
  102. return [
  103. ['!!in', '["08:00 Europe/Berlin","17:00 Europe/Berlin"]', 1, 'The given operator is invalid'],
  104. ['in', '["28:00 Europe/Berlin","17:00 Europe/Berlin"]', 2, 'The given time span is invalid'],
  105. ['in', '["08:00 Europe/Berlin","27:00 Europe/Berlin"]', 2, 'The given time span is invalid'],
  106. ['in', '["08:00 Europa/Berlin","17:00 Europe/Berlin"]', 3, 'The given start time is invalid'],
  107. ['in', '["08:00 Europe/Berlin","17:00 Europa/Berlin"]', 4, 'The given end time is invalid'],
  108. ['in', '["08:00 Europe/Bearlin","17:00 Europe/Berlin"]', 3, 'The given start time is invalid'],
  109. ['in', '["08:00 Europe/Berlin","17:00 Europe/Bearlin"]', 4, 'The given end time is invalid'],
  110. ];
  111. }
  112. /**
  113. * @dataProvider dataValidateCheckInvalid
  114. * @param string $operator
  115. * @param string $value
  116. * @param int $exceptionCode
  117. * @param string $exceptionMessage
  118. */
  119. public function testValidateCheckInvalid($operator, $value, $exceptionCode, $exceptionMessage) {
  120. $check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
  121. try {
  122. $check->validateCheck($operator, $value);
  123. } catch (\UnexpectedValueException $e) {
  124. $this->assertEquals($exceptionCode, $e->getCode());
  125. $this->assertEquals($exceptionMessage, $e->getMessage());
  126. }
  127. }
  128. }