FrontendDefinitionTraitTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_External\Tests;
  26. use OCA\Files_External\Lib\DefinitionParameter;
  27. use OCA\Files_External\Lib\StorageConfig;
  28. class FrontendDefinitionTraitTest extends \Test\TestCase {
  29. public function testJsonSerialization() {
  30. $param = $this->getMockBuilder(DefinitionParameter::class)
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $param->method('getName')->willReturn('foo');
  34. $trait = $this->getMockForTrait('\OCA\Files_External\Lib\FrontendDefinitionTrait');
  35. $trait->setText('test');
  36. $trait->addParameters([$param]);
  37. $trait->addCustomJs('foo/bar.js');
  38. $trait->addCustomJs('bar/foo.js');
  39. $json = $trait->jsonSerializeDefinition();
  40. $this->assertEquals('test', $json['name']);
  41. $this->assertContains('foo/bar.js', $json['custom']);
  42. $this->assertContains('bar/foo.js', $json['custom']);
  43. $configuration = $json['configuration'];
  44. $this->assertArrayHasKey('foo', $configuration);
  45. }
  46. public function validateStorageProvider() {
  47. return [
  48. [true, ['foo' => true, 'bar' => true, 'baz' => true]],
  49. [false, ['foo' => true, 'bar' => false]]
  50. ];
  51. }
  52. /**
  53. * @dataProvider validateStorageProvider
  54. */
  55. public function testValidateStorage($expectedSuccess, $params) {
  56. $backendParams = [];
  57. foreach ($params as $name => $valid) {
  58. $param = $this->getMockBuilder(DefinitionParameter::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $param->method('getName')
  62. ->willReturn($name);
  63. $param->method('isOptional')
  64. ->willReturn(false);
  65. $param->expects($this->once())
  66. ->method('validateValue')
  67. ->willReturn($valid);
  68. $backendParams[] = $param;
  69. }
  70. $storageConfig = $this->getMockBuilder(StorageConfig::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $storageConfig->expects($this->any())
  74. ->method('getBackendOption')
  75. ->willReturn(null);
  76. $storageConfig->expects($this->any())
  77. ->method('setBackendOption');
  78. $trait = $this->getMockForTrait('\OCA\Files_External\Lib\FrontendDefinitionTrait');
  79. $trait->setText('test');
  80. $trait->addParameters($backendParams);
  81. $this->assertEquals($expectedSuccess, $trait->validateStorageDefinition($storageConfig));
  82. }
  83. public function testValidateStorageSet() {
  84. $param = $this->getMockBuilder(DefinitionParameter::class)
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $param->method('getName')
  88. ->willReturn('param');
  89. $param->expects($this->once())
  90. ->method('validateValue')
  91. ->willReturnCallback(function (&$value) {
  92. $value = 'foobar';
  93. return true;
  94. });
  95. $storageConfig = $this->getMockBuilder(StorageConfig::class)
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $storageConfig->expects($this->once())
  99. ->method('getBackendOption')
  100. ->with('param')
  101. ->willReturn('barfoo');
  102. $storageConfig->expects($this->once())
  103. ->method('setBackendOption')
  104. ->with('param', 'foobar');
  105. $trait = $this->getMockForTrait('\OCA\Files_External\Lib\FrontendDefinitionTrait');
  106. $trait->setText('test');
  107. $trait->addParameter($param);
  108. $this->assertEquals(true, $trait->validateStorageDefinition($storageConfig));
  109. }
  110. }