Further modules included.
[yaffs-website] / web / modules / contrib / libraries / tests / src / Kernel / LibraryTypeKernelTestBase.php
1 <?php
2
3 namespace Drupal\Tests\libraries\Kernel;
4
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;
11
12 /**
13  * Provides an improved version of the core kernel test base class.
14  */
15 abstract class LibraryTypeKernelTestBase extends KernelTestBase {
16
17   /**
18    * The external library manager.
19    *
20    * @var \Drupal\libraries\ExternalLibrary\LibraryManagerInterface
21    */
22   protected $libraryManager;
23
24   /**
25    * The library type factory.
26    *
27    * @var \Drupal\Component\Plugin\Factory\FactoryInterface
28    */
29   protected $libraryTypeFactory;
30
31   /**
32    * The absolute path to the Libraries API module.
33    *
34    * @var string
35    */
36   protected $modulePath;
37
38   /**
39    * {@inheritdoc}
40    */
41   public static $modules = ['libraries', 'libraries_test'];
42
43   /**
44    * Gets the ID of the library type that is being tested.
45    *
46    * @return string
47    */
48   abstract protected function getLibraryTypeId();
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function setUp() {
54     parent::setUp();
55
56     /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
57     $module_handler = $this->container->get('module_handler');
58     $this->modulePath = $module_handler->getModule('libraries')->getPath();
59
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)
68       ->save();
69
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();
76
77     $this->libraryManager = $this->container->get('libraries.manager');
78     $this->libraryTypeFactory = $this->container->get('plugin.manager.libraries.library_type');
79   }
80
81   /**
82    * Tests that the library type can be instantiated.
83    */
84   public function testLibraryType() {
85     $type_id = $this->getLibraryTypeId();
86     try {
87       $this->libraryTypeFactory->createInstance($type_id);
88       $this->assertTrue(TRUE, "Library type '$type_id' can be instantiated.");
89     }
90     catch (PluginException $exception) {
91       $this->fail("Library type '$type_id' cannot be instantiated.");
92     }
93   }
94
95   /**
96    * Tests that the test library can be instantiated.
97    */
98   public function testLibrary() {
99     $type_id = $this->getLibraryTypeId();
100     $id = $this->getLibraryId();
101     try {
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());
106
107     }
108     catch (LibraryDefinitionNotFoundException $exception) {
109       $this->fail("Missing library definition for test $type_id library.");
110     }
111     catch (LibraryTypeNotFoundException $exception) {
112       $this->fail("Missing library type declaration for test $type_id library.");
113     }
114   }
115
116   /**
117    * Returns the library type that is being tested.
118    *
119    * @return \Drupal\libraries\ExternalLibrary\Type\LibraryTypeInterface
120    *   The test library type.
121    */
122   protected function getLibraryType() {
123     try {
124       $library_type = $this->libraryTypeFactory->createInstance($this->getLibraryTypeId());
125     }
126     catch (PluginException $exception) {
127       $library_type = $this->prophesize(LibraryTypeInterface::class)->reveal();
128     }
129     finally {
130       return $library_type;
131     }
132   }
133
134   /**
135    * Retuns the library ID of the library used in the test.
136    *
137    * Defaults to 'test_[library_type]_library', where [library_type] is the
138    * ID of the library type being tested.
139    *
140    * @return string
141    */
142   protected function getLibraryId() {
143     $type_id = $this->getLibraryTypeId();
144     return "test_{$type_id}_library";
145   }
146
147   /**
148    * Returns the test library for this library type.
149    *
150    * @return \Drupal\libraries\ExternalLibrary\LibraryInterface
151    *   The test library.
152    */
153   protected function getLibrary() {
154     try {
155       $library = $this->libraryManager->getLibrary($this->getLibraryId());
156     }
157     catch (LibraryDefinitionNotFoundException $exception) {
158       $library = $this->prophesize(LibraryInterface::class)->reveal();
159     }
160     catch (LibraryTypeNotFoundException $exception) {
161       $library = $this->prophesize(LibraryInterface::class)->reveal();
162     }
163     catch (PluginException $exception) {
164       $library = $this->prophesize(LibraryInterface::class)->reveal();
165     }
166     finally {
167       return $library;
168     }
169   }
170
171 }