RequestUserAgentTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 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\AbstractStringCheck;
  8. use OCA\WorkflowEngine\Check\RequestUserAgent;
  9. use OCP\IL10N;
  10. use OCP\IRequest;
  11. use Test\TestCase;
  12. class RequestUserAgentTest extends TestCase {
  13. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  14. protected $request;
  15. /** @var RequestUserAgent */
  16. protected $check;
  17. protected function setUp(): void {
  18. parent::setUp();
  19. $this->request = $this->createMock(IRequest::class);
  20. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject $l */
  21. $l = $this->getMockBuilder(IL10N::class)
  22. ->disableOriginalConstructor()
  23. ->getMock();
  24. $l->expects($this->any())
  25. ->method('t')
  26. ->willReturnCallback(function ($string, $args) {
  27. return sprintf($string, $args);
  28. });
  29. $this->check = new RequestUserAgent($l, $this->request);
  30. }
  31. public function dataExecuteCheck() {
  32. return [
  33. ['is', 'android', 'Mozilla/5.0 (Android) Nextcloud-android/2.2.0', true],
  34. ['is', 'android', 'Mozilla/5.0 (iOS) Nextcloud-iOS/2.2.0', false],
  35. ['is', 'android', 'Mozilla/5.0 (Linux) mirall/2.2.0', false],
  36. ['is', 'android', 'Mozilla/5.0 (Windows) Nextcloud-Outlook v2.2.0', false],
  37. ['is', 'android', 'Mozilla/5.0 (Linux) Nextcloud-Thunderbird v2.2.0', false],
  38. ['!is', 'android', 'Mozilla/5.0 (Android) Nextcloud-android/2.2.0', false],
  39. ['!is', 'android', 'Mozilla/5.0 (iOS) Nextcloud-iOS/2.2.0', true],
  40. ['!is', 'android', 'Mozilla/5.0 (Linux) mirall/2.2.0', true],
  41. ['!is', 'android', 'Mozilla/5.0 (Windows) Nextcloud-Outlook v2.2.0', true],
  42. ['!is', 'android', 'Mozilla/5.0 (Linux) Nextcloud-Thunderbird v2.2.0', true],
  43. ['is', 'ios', 'Mozilla/5.0 (Android) Nextcloud-android/2.2.0', false],
  44. ['is', 'ios', 'Mozilla/5.0 (iOS) Nextcloud-iOS/2.2.0', true],
  45. ['is', 'ios', 'Mozilla/5.0 (Linux) mirall/2.2.0', false],
  46. ['is', 'ios', 'Mozilla/5.0 (Windows) Nextcloud-Outlook v2.2.0', false],
  47. ['is', 'ios', 'Mozilla/5.0 (Linux) Nextcloud-Thunderbird v2.2.0', false],
  48. ['!is', 'ios', 'Mozilla/5.0 (Android) Nextcloud-android/2.2.0', true],
  49. ['!is', 'ios', 'Mozilla/5.0 (iOS) Nextcloud-iOS/2.2.0', false],
  50. ['!is', 'ios', 'Mozilla/5.0 (Linux) mirall/2.2.0', true],
  51. ['!is', 'ios', 'Mozilla/5.0 (Windows) Nextcloud-Outlook v2.2.0', true],
  52. ['!is', 'ios', 'Mozilla/5.0 (Linux) Nextcloud-Thunderbird v2.2.0', true],
  53. ['is', 'desktop', 'Mozilla/5.0 (Android) Nextcloud-android/2.2.0', false],
  54. ['is', 'desktop', 'Mozilla/5.0 (iOS) Nextcloud-iOS/2.2.0', false],
  55. ['is', 'desktop', 'Mozilla/5.0 (Linux) mirall/2.2.0', true],
  56. ['is', 'desktop', 'Mozilla/5.0 (Windows) Nextcloud-Outlook v2.2.0', false],
  57. ['is', 'desktop', 'Mozilla/5.0 (Linux) Nextcloud-Thunderbird v2.2.0', false],
  58. ['!is', 'desktop', 'Mozilla/5.0 (Android) Nextcloud-android/2.2.0', true],
  59. ['!is', 'desktop', 'Mozilla/5.0 (iOS) Nextcloud-iOS/2.2.0', true],
  60. ['!is', 'desktop', 'Mozilla/5.0 (Linux) mirall/2.2.0', false],
  61. ['!is', 'desktop', 'Mozilla/5.0 (Windows) Nextcloud-Outlook v2.2.0', true],
  62. ['!is', 'desktop', 'Mozilla/5.0 (Linux) Nextcloud-Thunderbird v2.2.0', true],
  63. ['is', 'mail', 'Mozilla/5.0 (Android) Nextcloud-android/2.2.0', false],
  64. ['is', 'mail', 'Mozilla/5.0 (iOS) Nextcloud-iOS/2.2.0', false],
  65. ['is', 'mail', 'Mozilla/5.0 (Linux) mirall/2.2.0', false],
  66. ['is', 'mail', 'Mozilla/5.0 (Windows) Nextcloud-Outlook v2.2.0', true],
  67. ['is', 'mail', 'Mozilla/5.0 (Linux) Nextcloud-Thunderbird v2.2.0', true],
  68. ['!is', 'mail', 'Mozilla/5.0 (Android) Nextcloud-android/2.2.0', true],
  69. ['!is', 'mail', 'Mozilla/5.0 (iOS) Nextcloud-iOS/2.2.0', true],
  70. ['!is', 'mail', 'Mozilla/5.0 (Linux) mirall/2.2.0', true],
  71. ['!is', 'mail', 'Mozilla/5.0 (Windows) Nextcloud-Outlook v2.2.0', false],
  72. ['!is', 'mail', 'Mozilla/5.0 (Linux) Nextcloud-Thunderbird v2.2.0', false],
  73. ];
  74. }
  75. /**
  76. * @dataProvider dataExecuteCheck
  77. * @param string $operation
  78. * @param string $checkValue
  79. * @param string $actualValue
  80. * @param bool $expected
  81. */
  82. public function testExecuteCheck($operation, $checkValue, $actualValue, $expected): void {
  83. $this->request->expects($this->once())
  84. ->method('getHeader')
  85. ->willReturn($actualValue);
  86. /** @var AbstractStringCheck $check */
  87. $this->assertEquals($expected, $this->check->executeCheck($operation, $checkValue));
  88. }
  89. }