FrontendDefinitionTraitTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests;
  8. use OCA\Files_External\Lib\DefinitionParameter;
  9. use OCA\Files_External\Lib\StorageConfig;
  10. class FrontendDefinitionTraitTest extends \Test\TestCase {
  11. public function testJsonSerialization() {
  12. $param = $this->getMockBuilder(DefinitionParameter::class)
  13. ->disableOriginalConstructor()
  14. ->getMock();
  15. $param->method('getName')->willReturn('foo');
  16. $trait = $this->getMockForTrait('\OCA\Files_External\Lib\FrontendDefinitionTrait');
  17. $trait->setText('test');
  18. $trait->addParameters([$param]);
  19. $trait->addCustomJs('foo/bar.js');
  20. $trait->addCustomJs('bar/foo.js');
  21. $json = $trait->jsonSerializeDefinition();
  22. $this->assertEquals('test', $json['name']);
  23. $this->assertContains('foo/bar.js', $json['custom']);
  24. $this->assertContains('bar/foo.js', $json['custom']);
  25. $configuration = $json['configuration'];
  26. $this->assertArrayHasKey('foo', $configuration);
  27. }
  28. public function validateStorageProvider() {
  29. return [
  30. [true, ['foo' => true, 'bar' => true, 'baz' => true]],
  31. [false, ['foo' => true, 'bar' => false]]
  32. ];
  33. }
  34. /**
  35. * @dataProvider validateStorageProvider
  36. */
  37. public function testValidateStorage($expectedSuccess, $params) {
  38. $backendParams = [];
  39. foreach ($params as $name => $valid) {
  40. $param = $this->getMockBuilder(DefinitionParameter::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $param->method('getName')
  44. ->willReturn($name);
  45. $param->method('isOptional')
  46. ->willReturn(false);
  47. $param->expects($this->once())
  48. ->method('validateValue')
  49. ->willReturn($valid);
  50. $backendParams[] = $param;
  51. }
  52. $storageConfig = $this->getMockBuilder(StorageConfig::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $storageConfig->expects($this->any())
  56. ->method('getBackendOption')
  57. ->willReturn(null);
  58. $storageConfig->expects($this->any())
  59. ->method('setBackendOption');
  60. $trait = $this->getMockForTrait('\OCA\Files_External\Lib\FrontendDefinitionTrait');
  61. $trait->setText('test');
  62. $trait->addParameters($backendParams);
  63. $this->assertEquals($expectedSuccess, $trait->validateStorageDefinition($storageConfig));
  64. }
  65. public function testValidateStorageSet() {
  66. $param = $this->getMockBuilder(DefinitionParameter::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $param->method('getName')
  70. ->willReturn('param');
  71. $param->expects($this->once())
  72. ->method('validateValue')
  73. ->willReturnCallback(function (&$value) {
  74. $value = 'foobar';
  75. return true;
  76. });
  77. $storageConfig = $this->getMockBuilder(StorageConfig::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $storageConfig->expects($this->once())
  81. ->method('getBackendOption')
  82. ->with('param')
  83. ->willReturn('barfoo');
  84. $storageConfig->expects($this->once())
  85. ->method('setBackendOption')
  86. ->with('param', 'foobar');
  87. $trait = $this->getMockForTrait('\OCA\Files_External\Lib\FrontendDefinitionTrait');
  88. $trait->setText('test');
  89. $trait->addParameter($param);
  90. $this->assertEquals(true, $trait->validateStorageDefinition($storageConfig));
  91. }
  92. }