3 namespace Drupal\Tests\libraries\Kernel;
5 use Drupal\Component\Plugin\Exception\PluginException;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException;
8 use Drupal\libraries\ExternalLibrary\Exception\LibraryTypeNotFoundException;
9 use Drupal\libraries\ExternalLibrary\LibraryInterface;
10 use Drupal\libraries\ExternalLibrary\Type\LibraryTypeInterface;
13 * Provides an improved version of the core kernel test base class.
15 abstract class LibraryTypeKernelTestBase extends KernelTestBase {
18 * The external library manager.
20 * @var \Drupal\libraries\ExternalLibrary\LibraryManagerInterface
22 protected $libraryManager;
25 * The library type factory.
27 * @var \Drupal\Component\Plugin\Factory\FactoryInterface
29 protected $libraryTypeFactory;
32 * The absolute path to the Libraries API module.
36 protected $modulePath;
41 public static $modules = ['libraries', 'libraries_test'];
44 * Gets the ID of the library type that is being tested.
48 abstract protected function getLibraryTypeId();
53 protected function setUp() {
56 /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
57 $module_handler = $this->container->get('module_handler');
58 $this->modulePath = $module_handler->getModule('libraries')->getPath();
60 $this->installConfig('libraries');
61 // Disable remote definition fetching and set the local definitions path to
62 // the module directory.
63 /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
64 $config_factory = $this->container->get('config.factory');
65 $config_factory->getEditable('libraries.settings')
66 ->set('definition.local.path', "{$this->modulePath}/tests/library_definitions")
67 ->set('definition.remote.enable', FALSE)
70 // LibrariesConfigSubscriber::onConfigSave() invalidates the container so
71 // that it is rebuilt on the next request. We need the container rebuilt
72 // immediately, however.
73 /** @var \Drupal\Core\DrupalKernelInterface $kernel */
74 $kernel = $this->container->get('kernel');
75 $this->container = $kernel->rebuildContainer();
77 $this->libraryManager = $this->container->get('libraries.manager');
78 $this->libraryTypeFactory = $this->container->get('plugin.manager.libraries.library_type');
82 * Tests that the library type can be instantiated.
84 public function testLibraryType() {
85 $type_id = $this->getLibraryTypeId();
87 $this->libraryTypeFactory->createInstance($type_id);
88 $this->assertTrue(TRUE, "Library type '$type_id' can be instantiated.");
90 catch (PluginException $exception) {
91 $this->fail("Library type '$type_id' cannot be instantiated.");
96 * Tests that the test library can be instantiated.
98 public function testLibrary() {
99 $type_id = $this->getLibraryTypeId();
100 $id = $this->getLibraryId();
102 $library = $this->libraryManager->getLibrary($id);
103 $this->assertTrue(TRUE, "Test $type_id library can be instantiated.");
104 $this->assertInstanceOf($this->getLibraryType()->getLibraryClass(), $library);
105 $this->assertEquals($this->getLibraryId(), $library->getId());
108 catch (LibraryDefinitionNotFoundException $exception) {
109 $this->fail("Missing library definition for test $type_id library.");
111 catch (LibraryTypeNotFoundException $exception) {
112 $this->fail("Missing library type declaration for test $type_id library.");
117 * Returns the library type that is being tested.
119 * @return \Drupal\libraries\ExternalLibrary\Type\LibraryTypeInterface
120 * The test library type.
122 protected function getLibraryType() {
124 $library_type = $this->libraryTypeFactory->createInstance($this->getLibraryTypeId());
126 catch (PluginException $exception) {
127 $library_type = $this->prophesize(LibraryTypeInterface::class)->reveal();
130 return $library_type;
135 * Retuns the library ID of the library used in the test.
137 * Defaults to 'test_[library_type]_library', where [library_type] is the
138 * ID of the library type being tested.
142 protected function getLibraryId() {
143 $type_id = $this->getLibraryTypeId();
144 return "test_{$type_id}_library";
148 * Returns the test library for this library type.
150 * @return \Drupal\libraries\ExternalLibrary\LibraryInterface
153 protected function getLibrary() {
155 $library = $this->libraryManager->getLibrary($this->getLibraryId());
157 catch (LibraryDefinitionNotFoundException $exception) {
158 $library = $this->prophesize(LibraryInterface::class)->reveal();
160 catch (LibraryTypeNotFoundException $exception) {
161 $library = $this->prophesize(LibraryInterface::class)->reveal();
163 catch (PluginException $exception) {
164 $library = $this->prophesize(LibraryInterface::class)->reveal();