FrontendDefinitionTraitTest.php 3.8 KB

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