annotate core/tests/Drupal/Tests/UnitTestCase.php @ 12:7a779792577d

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