FrontendDefinitionTraitTest.php 3.8 KB

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