Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\FileCache\FileCacheFactory;
|
Chris@0
|
6 use Drupal\Component\Utility\Random;
|
Chris@0
|
7 use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
|
Chris@0
|
8 use Drupal\Core\DependencyInjection\ContainerBuilder;
|
Chris@0
|
9 use Drupal\Core\StringTranslation\TranslatableMarkup;
|
Chris@0
|
10 use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
|
Chris@0
|
11 use PHPUnit\Framework\TestCase;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Provides a base class and helpers for Drupal unit tests.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @ingroup testing
|
Chris@0
|
17 */
|
Chris@0
|
18 abstract class UnitTestCase extends TestCase {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The random generator.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var \Drupal\Component\Utility\Random
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $randomGenerator;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The app root.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var string
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $root;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * {@inheritdoc}
|
Chris@0
|
36 */
|
Chris@0
|
37 protected function setUp() {
|
Chris@0
|
38 parent::setUp();
|
Chris@0
|
39 // Ensure that an instantiated container in the global state of \Drupal from
|
Chris@0
|
40 // a previous test does not leak into this test.
|
Chris@0
|
41 \Drupal::unsetContainer();
|
Chris@0
|
42
|
Chris@0
|
43 // Ensure that the NullFileCache implementation is used for the FileCache as
|
Chris@0
|
44 // unit tests should not be relying on caches implicitly.
|
Chris@0
|
45 FileCacheFactory::setConfiguration([FileCacheFactory::DISABLE_CACHE => TRUE]);
|
Chris@0
|
46 // Ensure that FileCacheFactory has a prefix.
|
Chris@0
|
47 FileCacheFactory::setPrefix('prefix');
|
Chris@0
|
48
|
Chris@0
|
49 $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Generates a unique random string containing letters and numbers.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @param int $length
|
Chris@0
|
56 * Length of random string to generate.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @return string
|
Chris@0
|
59 * Randomly generated unique string.
|
Chris@0
|
60 *
|
Chris@0
|
61 * @see \Drupal\Component\Utility\Random::name()
|
Chris@0
|
62 */
|
Chris@0
|
63 public function randomMachineName($length = 8) {
|
Chris@0
|
64 return $this->getRandomGenerator()->name($length, TRUE);
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * Gets the random generator for the utility methods.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @return \Drupal\Component\Utility\Random
|
Chris@0
|
71 * The random generator
|
Chris@0
|
72 */
|
Chris@0
|
73 protected function getRandomGenerator() {
|
Chris@0
|
74 if (!is_object($this->randomGenerator)) {
|
Chris@0
|
75 $this->randomGenerator = new Random();
|
Chris@0
|
76 }
|
Chris@0
|
77 return $this->randomGenerator;
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * Asserts if two arrays are equal by sorting them first.
|
Chris@0
|
82 *
|
Chris@0
|
83 * @param array $expected
|
Chris@0
|
84 * @param array $actual
|
Chris@0
|
85 * @param string $message
|
Chris@0
|
86 */
|
Chris@0
|
87 protected function assertArrayEquals(array $expected, array $actual, $message = NULL) {
|
Chris@0
|
88 ksort($expected);
|
Chris@0
|
89 ksort($actual);
|
Chris@0
|
90 $this->assertEquals($expected, $actual, $message);
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * Returns a stub config factory that behaves according to the passed in array.
|
Chris@0
|
95 *
|
Chris@0
|
96 * Use this to generate a config factory that will return the desired values
|
Chris@0
|
97 * for the given config names.
|
Chris@0
|
98 *
|
Chris@0
|
99 * @param array $configs
|
Chris@0
|
100 * An associative array of configuration settings whose keys are configuration
|
Chris@0
|
101 * object names and whose values are key => value arrays for the configuration
|
Chris@0
|
102 * object in question. Defaults to an empty array.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @return \PHPUnit_Framework_MockObject_MockBuilder
|
Chris@0
|
105 * A MockBuilder object for the ConfigFactory with the desired return values.
|
Chris@0
|
106 */
|
Chris@0
|
107 public function getConfigFactoryStub(array $configs = []) {
|
Chris@0
|
108 $config_get_map = [];
|
Chris@0
|
109 $config_editable_map = [];
|
Chris@0
|
110 // Construct the desired configuration object stubs, each with its own
|
Chris@0
|
111 // desired return map.
|
Chris@0
|
112 foreach ($configs as $config_name => $config_values) {
|
Chris@0
|
113 $map = [];
|
Chris@0
|
114 foreach ($config_values as $key => $value) {
|
Chris@0
|
115 $map[] = [$key, $value];
|
Chris@0
|
116 }
|
Chris@0
|
117 // Also allow to pass in no argument.
|
Chris@0
|
118 $map[] = ['', $config_values];
|
Chris@0
|
119
|
Chris@0
|
120 $immutable_config_object = $this->getMockBuilder('Drupal\Core\Config\ImmutableConfig')
|
Chris@0
|
121 ->disableOriginalConstructor()
|
Chris@0
|
122 ->getMock();
|
Chris@0
|
123 $immutable_config_object->expects($this->any())
|
Chris@0
|
124 ->method('get')
|
Chris@0
|
125 ->will($this->returnValueMap($map));
|
Chris@0
|
126 $config_get_map[] = [$config_name, $immutable_config_object];
|
Chris@0
|
127
|
Chris@0
|
128 $mutable_config_object = $this->getMockBuilder('Drupal\Core\Config\Config')
|
Chris@0
|
129 ->disableOriginalConstructor()
|
Chris@0
|
130 ->getMock();
|
Chris@0
|
131 $mutable_config_object->expects($this->any())
|
Chris@0
|
132 ->method('get')
|
Chris@0
|
133 ->will($this->returnValueMap($map));
|
Chris@0
|
134 $config_editable_map[] = [$config_name, $mutable_config_object];
|
Chris@0
|
135 }
|
Chris@0
|
136 // Construct a config factory with the array of configuration object stubs
|
Chris@0
|
137 // as its return map.
|
Chris@0
|
138 $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
|
Chris@0
|
139 $config_factory->expects($this->any())
|
Chris@0
|
140 ->method('get')
|
Chris@0
|
141 ->will($this->returnValueMap($config_get_map));
|
Chris@0
|
142 $config_factory->expects($this->any())
|
Chris@0
|
143 ->method('getEditable')
|
Chris@0
|
144 ->will($this->returnValueMap($config_editable_map));
|
Chris@0
|
145 return $config_factory;
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 /**
|
Chris@0
|
149 * Returns a stub config storage that returns the supplied configuration.
|
Chris@0
|
150 *
|
Chris@0
|
151 * @param array $configs
|
Chris@0
|
152 * An associative array of configuration settings whose keys are
|
Chris@0
|
153 * configuration object names and whose values are key => value arrays
|
Chris@0
|
154 * for the configuration object in question.
|
Chris@0
|
155 *
|
Chris@0
|
156 * @return \Drupal\Core\Config\StorageInterface
|
Chris@0
|
157 * A mocked config storage.
|
Chris@0
|
158 */
|
Chris@0
|
159 public function getConfigStorageStub(array $configs) {
|
Chris@0
|
160 $config_storage = $this->getMock('Drupal\Core\Config\NullStorage');
|
Chris@0
|
161 $config_storage->expects($this->any())
|
Chris@0
|
162 ->method('listAll')
|
Chris@0
|
163 ->will($this->returnValue(array_keys($configs)));
|
Chris@0
|
164
|
Chris@0
|
165 foreach ($configs as $name => $config) {
|
Chris@0
|
166 $config_storage->expects($this->any())
|
Chris@0
|
167 ->method('read')
|
Chris@0
|
168 ->with($this->equalTo($name))
|
Chris@0
|
169 ->will($this->returnValue($config));
|
Chris@0
|
170 }
|
Chris@0
|
171 return $config_storage;
|
Chris@0
|
172 }
|
Chris@0
|
173
|
Chris@0
|
174 /**
|
Chris@0
|
175 * Mocks a block with a block plugin.
|
Chris@0
|
176 *
|
Chris@0
|
177 * @param string $machine_name
|
Chris@0
|
178 * The machine name of the block plugin.
|
Chris@0
|
179 *
|
Chris@0
|
180 * @return \Drupal\block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject
|
Chris@0
|
181 * The mocked block.
|
Chris@0
|
182 */
|
Chris@0
|
183 protected function getBlockMockWithMachineName($machine_name) {
|
Chris@0
|
184 $plugin = $this->getMockBuilder('Drupal\Core\Block\BlockBase')
|
Chris@0
|
185 ->disableOriginalConstructor()
|
Chris@0
|
186 ->getMock();
|
Chris@0
|
187 $plugin->expects($this->any())
|
Chris@0
|
188 ->method('getMachineNameSuggestion')
|
Chris@0
|
189 ->will($this->returnValue($machine_name));
|
Chris@0
|
190
|
Chris@0
|
191 $block = $this->getMockBuilder('Drupal\block\Entity\Block')
|
Chris@0
|
192 ->disableOriginalConstructor()
|
Chris@0
|
193 ->getMock();
|
Chris@0
|
194 $block->expects($this->any())
|
Chris@0
|
195 ->method('getPlugin')
|
Chris@0
|
196 ->will($this->returnValue($plugin));
|
Chris@0
|
197 return $block;
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 /**
|
Chris@0
|
201 * Returns a stub translation manager that just returns the passed string.
|
Chris@0
|
202 *
|
Chris@0
|
203 * @return \PHPUnit_Framework_MockObject_MockObject|\Drupal\Core\StringTranslation\TranslationInterface
|
Chris@0
|
204 * A mock translation object.
|
Chris@0
|
205 */
|
Chris@0
|
206 public function getStringTranslationStub() {
|
Chris@0
|
207 $translation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
|
Chris@0
|
208 $translation->expects($this->any())
|
Chris@0
|
209 ->method('translate')
|
Chris@0
|
210 ->willReturnCallback(function ($string, array $args = [], array $options = []) use ($translation) {
|
Chris@0
|
211 return new TranslatableMarkup($string, $args, $options, $translation);
|
Chris@0
|
212 });
|
Chris@0
|
213 $translation->expects($this->any())
|
Chris@0
|
214 ->method('translateString')
|
Chris@0
|
215 ->willReturnCallback(function (TranslatableMarkup $wrapper) {
|
Chris@0
|
216 return $wrapper->getUntranslatedString();
|
Chris@0
|
217 });
|
Chris@0
|
218 $translation->expects($this->any())
|
Chris@0
|
219 ->method('formatPlural')
|
Chris@0
|
220 ->willReturnCallback(function ($count, $singular, $plural, array $args = [], array $options = []) use ($translation) {
|
Chris@0
|
221 $wrapper = new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $translation);
|
Chris@0
|
222 return $wrapper;
|
Chris@0
|
223 });
|
Chris@0
|
224 return $translation;
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 /**
|
Chris@0
|
228 * Sets up a container with a cache tags invalidator.
|
Chris@0
|
229 *
|
Chris@0
|
230 * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_validator
|
Chris@0
|
231 * The cache tags invalidator.
|
Chris@0
|
232 *
|
Chris@0
|
233 * @return \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject
|
Chris@0
|
234 * The container with the cache tags invalidator service.
|
Chris@0
|
235 */
|
Chris@0
|
236 protected function getContainerWithCacheTagsInvalidator(CacheTagsInvalidatorInterface $cache_tags_validator) {
|
Chris@0
|
237 $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
|
Chris@0
|
238 $container->expects($this->any())
|
Chris@0
|
239 ->method('get')
|
Chris@0
|
240 ->with('cache_tags.invalidator')
|
Chris@0
|
241 ->will($this->returnValue($cache_tags_validator));
|
Chris@0
|
242
|
Chris@0
|
243 \Drupal::setContainer($container);
|
Chris@0
|
244 return $container;
|
Chris@0
|
245 }
|
Chris@0
|
246
|
Chris@0
|
247 /**
|
Chris@0
|
248 * Returns a stub class resolver.
|
Chris@0
|
249 *
|
Chris@0
|
250 * @return \Drupal\Core\DependencyInjection\ClassResolverInterface|\PHPUnit_Framework_MockObject_MockObject
|
Chris@0
|
251 * The class resolver stub.
|
Chris@0
|
252 */
|
Chris@0
|
253 protected function getClassResolverStub() {
|
Chris@0
|
254 $class_resolver = $this->getMock('Drupal\Core\DependencyInjection\ClassResolverInterface');
|
Chris@0
|
255 $class_resolver->expects($this->any())
|
Chris@0
|
256 ->method('getInstanceFromDefinition')
|
Chris@0
|
257 ->will($this->returnCallback(function ($class) {
|
Chris@0
|
258 if (is_subclass_of($class, 'Drupal\Core\DependencyInjection\ContainerInjectionInterface')) {
|
Chris@0
|
259 return $class::create(new ContainerBuilder());
|
Chris@0
|
260 }
|
Chris@0
|
261 else {
|
Chris@0
|
262 return new $class();
|
Chris@0
|
263 }
|
Chris@0
|
264 }));
|
Chris@0
|
265 return $class_resolver;
|
Chris@0
|
266 }
|
Chris@0
|
267
|
Chris@0
|
268 }
|