RequestTimeTest.php 6.4 KB

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