ActionTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Notification;
  8. use OC\Notification\Action;
  9. use OCP\Notification\IAction;
  10. use Test\TestCase;
  11. class ActionTest extends TestCase {
  12. /** @var IAction */
  13. protected $action;
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->action = new Action();
  17. }
  18. public function dataSetLabel() {
  19. return [
  20. ['test1'],
  21. [str_repeat('a', 1)],
  22. [str_repeat('a', 32)],
  23. ];
  24. }
  25. /**
  26. * @dataProvider dataSetLabel
  27. * @param string $label
  28. */
  29. public function testSetLabel($label) {
  30. $this->assertSame('', $this->action->getLabel());
  31. $this->assertSame($this->action, $this->action->setLabel($label));
  32. $this->assertSame($label, $this->action->getLabel());
  33. }
  34. public function dataSetLabelInvalid() {
  35. return [
  36. [''],
  37. [str_repeat('a', 33)],
  38. ];
  39. }
  40. /**
  41. * @dataProvider dataSetLabelInvalid
  42. * @param mixed $label
  43. *
  44. */
  45. public function testSetLabelInvalid($label) {
  46. $this->expectException(\InvalidArgumentException::class);
  47. $this->action->setLabel($label);
  48. }
  49. public function dataSetParsedLabel() {
  50. return [
  51. ['test1'],
  52. [str_repeat('a', 1)],
  53. [str_repeat('a', 32)],
  54. ];
  55. }
  56. /**
  57. * @dataProvider dataSetParsedLabel
  58. * @param string $label
  59. */
  60. public function testSetParsedLabel($label) {
  61. $this->assertSame('', $this->action->getParsedLabel());
  62. $this->assertSame($this->action, $this->action->setParsedLabel($label));
  63. $this->assertSame($label, $this->action->getParsedLabel());
  64. }
  65. public function dataSetParsedLabelInvalid() {
  66. return [
  67. [''],
  68. ];
  69. }
  70. /**
  71. * @dataProvider dataSetParsedLabelInvalid
  72. * @param mixed $label
  73. *
  74. */
  75. public function testSetParsedLabelInvalid($label) {
  76. $this->expectException(\InvalidArgumentException::class);
  77. $this->action->setParsedLabel($label);
  78. }
  79. public function dataSetLink() {
  80. return [
  81. ['test1', 'GET'],
  82. ['test2', 'POST'],
  83. [str_repeat('a', 1), 'PUT'],
  84. [str_repeat('a', 256), 'DELETE'],
  85. ];
  86. }
  87. /**
  88. * @dataProvider dataSetLink
  89. * @param string $link
  90. * @param string $type
  91. */
  92. public function testSetLink($link, $type) {
  93. $this->assertSame('', $this->action->getLink());
  94. $this->assertSame($this->action, $this->action->setLink($link, $type));
  95. $this->assertSame($link, $this->action->getLink());
  96. $this->assertSame($type, $this->action->getRequestType());
  97. }
  98. public function dataSetLinkInvalid() {
  99. return [
  100. // Invalid link
  101. ['', 'GET'],
  102. [str_repeat('a', 257), 'GET'],
  103. // Invalid type
  104. ['url', 'notGET'],
  105. ];
  106. }
  107. /**
  108. * @dataProvider dataSetLinkInvalid
  109. * @param mixed $link
  110. * @param mixed $type
  111. *
  112. */
  113. public function testSetLinkInvalid($link, $type) {
  114. $this->expectException(\InvalidArgumentException::class);
  115. $this->action->setLink($link, $type);
  116. }
  117. public function dataSetPrimary() {
  118. return [
  119. [true],
  120. [false],
  121. ];
  122. }
  123. /**
  124. * @dataProvider dataSetPrimary
  125. * @param bool $primary
  126. */
  127. public function testSetPrimary($primary) {
  128. $this->assertSame(false, $this->action->isPrimary());
  129. $this->assertSame($this->action, $this->action->setPrimary($primary));
  130. $this->assertSame($primary, $this->action->isPrimary());
  131. }
  132. public function testIsValid() {
  133. $this->assertFalse($this->action->isValid());
  134. $this->assertFalse($this->action->isValidParsed());
  135. $this->action->setLabel('label');
  136. $this->assertFalse($this->action->isValid());
  137. $this->assertFalse($this->action->isValidParsed());
  138. $this->action->setLink('link', 'GET');
  139. $this->assertTrue($this->action->isValid());
  140. $this->assertFalse($this->action->isValidParsed());
  141. }
  142. public function testIsValidParsed() {
  143. $this->assertFalse($this->action->isValid());
  144. $this->assertFalse($this->action->isValidParsed());
  145. $this->action->setParsedLabel('label');
  146. $this->assertFalse($this->action->isValid());
  147. $this->assertFalse($this->action->isValidParsed());
  148. $this->action->setLink('link', 'GET');
  149. $this->assertFalse($this->action->isValid());
  150. $this->assertTrue($this->action->isValidParsed());
  151. }
  152. }