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