Chris@0: TRUE]); Chris@0: // Ensure that FileCacheFactory has a prefix. Chris@0: FileCacheFactory::setPrefix('prefix'); Chris@0: Chris@0: $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)))); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates a unique random string containing letters and numbers. Chris@0: * Chris@0: * @param int $length Chris@0: * Length of random string to generate. Chris@0: * Chris@0: * @return string Chris@0: * Randomly generated unique string. Chris@0: * Chris@0: * @see \Drupal\Component\Utility\Random::name() Chris@0: */ Chris@0: public function randomMachineName($length = 8) { Chris@0: return $this->getRandomGenerator()->name($length, TRUE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the random generator for the utility methods. Chris@0: * Chris@0: * @return \Drupal\Component\Utility\Random Chris@0: * The random generator Chris@0: */ Chris@0: protected function getRandomGenerator() { Chris@0: if (!is_object($this->randomGenerator)) { Chris@0: $this->randomGenerator = new Random(); Chris@0: } Chris@0: return $this->randomGenerator; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts if two arrays are equal by sorting them first. Chris@0: * Chris@0: * @param array $expected Chris@0: * @param array $actual Chris@0: * @param string $message Chris@0: */ Chris@0: protected function assertArrayEquals(array $expected, array $actual, $message = NULL) { Chris@0: ksort($expected); Chris@0: ksort($actual); Chris@0: $this->assertEquals($expected, $actual, $message); Chris@0: } Chris@0: Chris@0: /** Chris@14: * Returns a stub config factory that behaves according to the passed array. Chris@0: * Chris@0: * Use this to generate a config factory that will return the desired values Chris@0: * for the given config names. Chris@0: * Chris@0: * @param array $configs Chris@14: * An associative array of configuration settings whose keys are Chris@14: * configuration object names and whose values are key => value arrays for Chris@14: * the configuration object in question. Defaults to an empty array. Chris@0: * Chris@0: * @return \PHPUnit_Framework_MockObject_MockBuilder Chris@14: * A MockBuilder object for the ConfigFactory with the desired return Chris@14: * values. Chris@0: */ Chris@0: public function getConfigFactoryStub(array $configs = []) { Chris@0: $config_get_map = []; Chris@0: $config_editable_map = []; Chris@0: // Construct the desired configuration object stubs, each with its own Chris@0: // desired return map. Chris@0: foreach ($configs as $config_name => $config_values) { Chris@14: // Define a closure over the $config_values, which will be used as a Chris@14: // returnCallback below. This function will mimic Chris@14: // \Drupal\Core\Config\Config::get and allow using dotted keys. Chris@14: $config_get = function ($key = '') use ($config_values) { Chris@14: // Allow to pass in no argument. Chris@14: if (empty($key)) { Chris@14: return $config_values; Chris@14: } Chris@14: // See if we have the key as is. Chris@14: if (isset($config_values[$key])) { Chris@14: return $config_values[$key]; Chris@14: } Chris@14: $parts = explode('.', $key); Chris@14: $value = NestedArray::getValue($config_values, $parts, $key_exists); Chris@14: return $key_exists ? $value : NULL; Chris@14: }; Chris@0: Chris@0: $immutable_config_object = $this->getMockBuilder('Drupal\Core\Config\ImmutableConfig') Chris@0: ->disableOriginalConstructor() Chris@0: ->getMock(); Chris@0: $immutable_config_object->expects($this->any()) Chris@0: ->method('get') Chris@14: ->will($this->returnCallback($config_get)); Chris@0: $config_get_map[] = [$config_name, $immutable_config_object]; Chris@0: Chris@0: $mutable_config_object = $this->getMockBuilder('Drupal\Core\Config\Config') Chris@0: ->disableOriginalConstructor() Chris@0: ->getMock(); Chris@0: $mutable_config_object->expects($this->any()) Chris@0: ->method('get') Chris@14: ->will($this->returnCallback($config_get)); Chris@0: $config_editable_map[] = [$config_name, $mutable_config_object]; Chris@0: } Chris@0: // Construct a config factory with the array of configuration object stubs Chris@0: // as its return map. Chris@12: $config_factory = $this->createMock('Drupal\Core\Config\ConfigFactoryInterface'); Chris@0: $config_factory->expects($this->any()) Chris@0: ->method('get') Chris@0: ->will($this->returnValueMap($config_get_map)); Chris@0: $config_factory->expects($this->any()) Chris@0: ->method('getEditable') Chris@0: ->will($this->returnValueMap($config_editable_map)); Chris@0: return $config_factory; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a stub config storage that returns the supplied configuration. Chris@0: * Chris@0: * @param array $configs Chris@0: * An associative array of configuration settings whose keys are Chris@0: * configuration object names and whose values are key => value arrays Chris@0: * for the configuration object in question. Chris@0: * Chris@0: * @return \Drupal\Core\Config\StorageInterface Chris@0: * A mocked config storage. Chris@0: */ Chris@0: public function getConfigStorageStub(array $configs) { Chris@12: $config_storage = $this->createMock('Drupal\Core\Config\NullStorage'); Chris@0: $config_storage->expects($this->any()) Chris@0: ->method('listAll') Chris@0: ->will($this->returnValue(array_keys($configs))); Chris@0: Chris@0: foreach ($configs as $name => $config) { Chris@0: $config_storage->expects($this->any()) Chris@0: ->method('read') Chris@0: ->with($this->equalTo($name)) Chris@0: ->will($this->returnValue($config)); Chris@0: } Chris@0: return $config_storage; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Mocks a block with a block plugin. Chris@0: * Chris@0: * @param string $machine_name Chris@0: * The machine name of the block plugin. Chris@0: * Chris@0: * @return \Drupal\block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject Chris@0: * The mocked block. Chris@14: * Chris@14: * @deprecated in Drupal 8.5.x, will be removed before Drupal 9.0.0. Unit test Chris@14: * base classes should not have dependencies on extensions. Set up mocks in Chris@14: * individual tests. Chris@14: * Chris@14: * @see https://www.drupal.org/node/2896072 Chris@0: */ Chris@0: protected function getBlockMockWithMachineName($machine_name) { Chris@0: $plugin = $this->getMockBuilder('Drupal\Core\Block\BlockBase') Chris@0: ->disableOriginalConstructor() Chris@0: ->getMock(); Chris@0: $plugin->expects($this->any()) Chris@0: ->method('getMachineNameSuggestion') Chris@0: ->will($this->returnValue($machine_name)); Chris@0: Chris@0: $block = $this->getMockBuilder('Drupal\block\Entity\Block') Chris@0: ->disableOriginalConstructor() Chris@0: ->getMock(); Chris@0: $block->expects($this->any()) Chris@0: ->method('getPlugin') Chris@0: ->will($this->returnValue($plugin)); Chris@14: @trigger_error(__METHOD__ . ' is deprecated in Drupal 8.5.x, will be removed before Drupal 9.0.0. Unit test base classes should not have dependencies on extensions. Set up mocks in individual tests.', E_USER_DEPRECATED); Chris@0: return $block; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a stub translation manager that just returns the passed string. Chris@0: * Chris@0: * @return \PHPUnit_Framework_MockObject_MockObject|\Drupal\Core\StringTranslation\TranslationInterface Chris@0: * A mock translation object. Chris@0: */ Chris@0: public function getStringTranslationStub() { Chris@12: $translation = $this->createMock('Drupal\Core\StringTranslation\TranslationInterface'); Chris@0: $translation->expects($this->any()) Chris@0: ->method('translate') Chris@0: ->willReturnCallback(function ($string, array $args = [], array $options = []) use ($translation) { Chris@0: return new TranslatableMarkup($string, $args, $options, $translation); Chris@0: }); Chris@0: $translation->expects($this->any()) Chris@0: ->method('translateString') Chris@0: ->willReturnCallback(function (TranslatableMarkup $wrapper) { Chris@0: return $wrapper->getUntranslatedString(); Chris@0: }); Chris@0: $translation->expects($this->any()) Chris@0: ->method('formatPlural') Chris@0: ->willReturnCallback(function ($count, $singular, $plural, array $args = [], array $options = []) use ($translation) { Chris@0: $wrapper = new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $translation); Chris@0: return $wrapper; Chris@0: }); Chris@0: return $translation; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets up a container with a cache tags invalidator. Chris@0: * Chris@0: * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_validator Chris@0: * The cache tags invalidator. Chris@0: * Chris@0: * @return \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject Chris@0: * The container with the cache tags invalidator service. Chris@0: */ Chris@0: protected function getContainerWithCacheTagsInvalidator(CacheTagsInvalidatorInterface $cache_tags_validator) { Chris@12: $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface'); Chris@0: $container->expects($this->any()) Chris@0: ->method('get') Chris@0: ->with('cache_tags.invalidator') Chris@0: ->will($this->returnValue($cache_tags_validator)); Chris@0: Chris@0: \Drupal::setContainer($container); Chris@0: return $container; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a stub class resolver. Chris@0: * Chris@0: * @return \Drupal\Core\DependencyInjection\ClassResolverInterface|\PHPUnit_Framework_MockObject_MockObject Chris@0: * The class resolver stub. Chris@0: */ Chris@0: protected function getClassResolverStub() { Chris@12: $class_resolver = $this->createMock('Drupal\Core\DependencyInjection\ClassResolverInterface'); Chris@0: $class_resolver->expects($this->any()) Chris@0: ->method('getInstanceFromDefinition') Chris@0: ->will($this->returnCallback(function ($class) { Chris@0: if (is_subclass_of($class, 'Drupal\Core\DependencyInjection\ContainerInjectionInterface')) { Chris@0: return $class::create(new ContainerBuilder()); Chris@0: } Chris@0: else { Chris@0: return new $class(); Chris@0: } Chris@0: })); Chris@0: return $class_resolver; Chris@0: } Chris@0: Chris@0: }