ResourceLocatorTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. ->willReturnCallback(function ($key, $default = null) use ($theme) {
  28. if ($key === 'theme') {
  29. return $theme;
  30. }
  31. return $default;
  32. });
  33. $this->overwriteService(SystemConfig::class, $systemConfig);
  34. return $this->getMockForAbstractClass('OC\Template\ResourceLocator',
  35. [$this->logger],
  36. '', true, true, true, []);
  37. }
  38. public function testFind() {
  39. $locator = $this->getResourceLocator('theme');
  40. $locator->expects($this->once())
  41. ->method('doFind')
  42. ->with('foo');
  43. $locator->expects($this->once())
  44. ->method('doFindTheme')
  45. ->with('foo');
  46. /** @var \OC\Template\ResourceLocator $locator */
  47. $locator->find(['foo']);
  48. }
  49. public function testFindNotFound() {
  50. $systemConfig = $this->createMock(SystemConfig::class);
  51. $systemConfig->method('getValue')
  52. ->with('theme', '')
  53. ->willReturn('theme');
  54. $this->overwriteService(SystemConfig::class, $systemConfig);
  55. $locator = $this->getResourceLocator('theme',
  56. ['core' => 'map'], ['3rd' => 'party'], ['foo' => 'bar']);
  57. $locator->expects($this->once())
  58. ->method('doFind')
  59. ->with('foo')
  60. ->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
  61. $locator->expects($this->once())
  62. ->method('doFindTheme')
  63. ->with('foo')
  64. ->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
  65. $this->logger->expects($this->exactly(2))
  66. ->method('debug')
  67. ->with($this->stringContains('map/foo'));
  68. /** @var \OC\Template\ResourceLocator $locator */
  69. $locator->find(['foo']);
  70. }
  71. public function testAppendIfExist() {
  72. $locator = $this->getResourceLocator('theme');
  73. /** @var \OC\Template\ResourceLocator $locator */
  74. $method = new \ReflectionMethod($locator, 'appendIfExist');
  75. $method->setAccessible(true);
  76. $method->invoke($locator, __DIR__, basename(__FILE__), 'webroot');
  77. $resource1 = [__DIR__, 'webroot', basename(__FILE__)];
  78. $this->assertEquals([$resource1], $locator->getResources());
  79. $method->invoke($locator, __DIR__, 'does-not-exist');
  80. $this->assertEquals([$resource1], $locator->getResources());
  81. }
  82. }