1
0

frontenddefinitiontraittest.php 3.7 KB

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