LibrariesDirectoryFileFinderTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Drupal\Tests\Core\Asset;
  3. use Drupal\Core\Asset\LibrariesDirectoryFileFinder;
  4. use Drupal\Core\Extension\ProfileExtensionList;
  5. use Drupal\Tests\UnitTestCase;
  6. use org\bovigo\vfs\vfsStream;
  7. /**
  8. * @coversDefaultClass \Drupal\Core\Asset\LibrariesDirectoryFileFinder
  9. * @group Asset
  10. */
  11. class LibrariesDirectoryFileFinderTest extends UnitTestCase {
  12. /**
  13. * @covers ::find
  14. */
  15. public function testFind() {
  16. // Place a library file in all the possible locations.
  17. $structure = [
  18. 'sites' => [
  19. 'example.com' => [
  20. 'libraries' => [
  21. 'third_party_library' => [
  22. 'css' => [
  23. 'example.css' => '/*Some css*/',
  24. ],
  25. ],
  26. ],
  27. ],
  28. ],
  29. 'libraries' => [
  30. 'third_party_library' => [
  31. 'css' => [
  32. 'example.css' => '/*Some css*/',
  33. ],
  34. ],
  35. ],
  36. 'profiles' => [
  37. 'library_testing' => [
  38. 'libraries' => [
  39. 'third_party_library' => [
  40. 'css' => [
  41. 'example.css' => '/*Some css*/',
  42. ],
  43. ],
  44. ],
  45. ],
  46. ],
  47. ];
  48. vfsStream::setup('root', NULL, $structure);
  49. $extension_list = $this->prophesize(ProfileExtensionList::class);
  50. $extension_list->getPath('library_testing')->willReturn('profiles/library_testing');
  51. $finder = new LibrariesDirectoryFileFinder('vfs://root', 'sites/example.com', $extension_list->reveal(), 'library_testing');
  52. // The site specific location is the first location used.
  53. $path = $finder->find('third_party_library/css/example.css');
  54. $this->assertEquals('sites/example.com/libraries/third_party_library/css/example.css', $path);
  55. // After removing the site specific location the root libraries folder
  56. // should be used.
  57. unlink('vfs://root/sites/example.com/libraries/third_party_library/css/example.css');
  58. $path = $finder->find('third_party_library/css/example.css');
  59. $this->assertEquals('libraries/third_party_library/css/example.css', $path);
  60. // The profile's libraries is now the only remaining location.
  61. unlink('vfs://root/libraries/third_party_library/css/example.css');
  62. $path = $finder->find('third_party_library/css/example.css');
  63. $this->assertEquals('profiles/library_testing/libraries/third_party_library/css/example.css', $path);
  64. // If the libraries file cannot be found FALSE is returned.
  65. unlink('vfs://root/profiles/library_testing/libraries/third_party_library/css/example.css');
  66. $this->assertFalse($finder->find('third_party_library/css/example.css'));
  67. // Test finding by the directory only. As all the directories still we'll
  68. // find the first location.
  69. $path = $finder->find('third_party_library');
  70. $this->assertEquals('sites/example.com/libraries/third_party_library', $path);
  71. }
  72. }
  73. if (!defined('DRUPAL_MINIMUM_PHP')) {
  74. define('DRUPAL_MINIMUM_PHP', '7.0.8');
  75. }