You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.7 KiB

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