RequestUserAgentTest.php 5.0 KB

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