ResourceLocatorTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Template;
  8. use OC\SystemConfig;
  9. use OC\Template\ResourceNotFoundException;
  10. use Psr\Log\LoggerInterface;
  11. class ResourceLocatorTest extends \Test\TestCase {
  12. /** @var \PHPUnit\Framework\MockObject\MockObject */
  13. protected $logger;
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->logger = $this->createMock(LoggerInterface::class);
  17. }
  18. /**
  19. * @param string $theme
  20. * @return \PHPUnit\Framework\MockObject\MockObject
  21. */
  22. public function getResourceLocator($theme) {
  23. $systemConfig = $this->createMock(SystemConfig::class);
  24. $systemConfig
  25. ->expects($this->any())
  26. ->method('getValue')
  27. ->with('theme', '')
  28. ->willReturn($theme);
  29. $this->overwriteService(SystemConfig::class, $systemConfig);
  30. return $this->getMockForAbstractClass('OC\Template\ResourceLocator',
  31. [$this->logger],
  32. '', true, true, true, []);
  33. }
  34. public function testFind() {
  35. $locator = $this->getResourceLocator('theme');
  36. $locator->expects($this->once())
  37. ->method('doFind')
  38. ->with('foo');
  39. $locator->expects($this->once())
  40. ->method('doFindTheme')
  41. ->with('foo');
  42. /** @var \OC\Template\ResourceLocator $locator */
  43. $locator->find(['foo']);
  44. }
  45. public function testFindNotFound() {
  46. $systemConfig = $this->createMock(SystemConfig::class);
  47. $systemConfig->method('getValue')
  48. ->with('theme', '')
  49. ->willReturn('theme');
  50. $this->overwriteService(SystemConfig::class, $systemConfig);
  51. $locator = $this->getResourceLocator('theme',
  52. ['core' => 'map'], ['3rd' => 'party'], ['foo' => 'bar']);
  53. $locator->expects($this->once())
  54. ->method('doFind')
  55. ->with('foo')
  56. ->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
  57. $locator->expects($this->once())
  58. ->method('doFindTheme')
  59. ->with('foo')
  60. ->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
  61. $this->logger->expects($this->exactly(2))
  62. ->method('debug')
  63. ->with($this->stringContains('map/foo'));
  64. /** @var \OC\Template\ResourceLocator $locator */
  65. $locator->find(['foo']);
  66. }
  67. public function testAppendIfExist() {
  68. $locator = $this->getResourceLocator('theme');
  69. /** @var \OC\Template\ResourceLocator $locator */
  70. $method = new \ReflectionMethod($locator, 'appendIfExist');
  71. $method->setAccessible(true);
  72. $method->invoke($locator, __DIR__, basename(__FILE__), 'webroot');
  73. $resource1 = [__DIR__, 'webroot', basename(__FILE__)];
  74. $this->assertEquals([$resource1], $locator->getResources());
  75. $method->invoke($locator, __DIR__, 'does-not-exist');
  76. $this->assertEquals([$resource1], $locator->getResources());
  77. }
  78. }