/**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     // Mock a Discovery object to replace AnnotationClassDiscovery.
     $this->discovery = $this->getMock('Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface');
     $this->discovery->expects($this->any())->method('getDefinitions')->will($this->returnValue($this->definitions));
 }
 /**
  * @covers ::getDefinitions
  */
 public function testGetDefinitions()
 {
     $definitions = array('foo' => array('label' => $this->randomMachineName()));
     $this->discovery->expects($this->once())->method('getDefinitions')->willReturn($definitions);
     $this->moduleHandler->expects($this->once())->method('alter')->with('payment_line_item');
     $this->assertSame($definitions, $this->sut->getDefinitions());
 }
 /**
  * {@inheritdoc}
  */
 public function getDefinitions()
 {
     if (is_null($this->pluginDefinitions) || !$this->useCaches) {
         $this->pluginDefinitions = $this->processDecoratedDefinitions($this->decoratedDiscovery->getDefinitions());
     }
     return $this->pluginDefinitions;
 }
 /**
  * {@inheritdoc}
  */
 public function getDefinitions()
 {
     if (isset($this->registerDefinitions)) {
         call_user_func($this->registerDefinitions);
     }
     $this->definitions += $this->decorated->getDefinitions();
     return parent::getDefinitions();
 }
 /**
  * {@inheritdoc}
  */
 public function getDefinitions()
 {
     /** @var \Drupal\Component\Plugin\PluginManagerInterface $source_plugin_manager */
     $source_plugin_manager = \Drupal::service('plugin.manager.migrate.source');
     return array_filter($this->decorated->getDefinitions(), function (array $definition) use($source_plugin_manager) {
         return $source_plugin_manager->hasDefinition($definition['source']['plugin']);
     });
 }
 /**
  * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinitions().
  */
 public function getDefinitions()
 {
     $definitions = $this->decorated->getDefinitions();
     foreach (\Drupal::moduleHandler()->getImplementations($this->hook) as $module) {
         $function = $module . '_' . $this->hook;
         $function($definitions);
     }
     return $definitions;
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     // Prepare the default constructor arguments required by MailManager.
     $this->cache = $this->getMock('Drupal\\Core\\Cache\\CacheBackendInterface');
     $this->moduleHandler = $this->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
     // Mock a Discovery object to replace AnnotationClassDiscovery.
     $this->discovery = $this->getMock('Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface');
     $this->discovery->expects($this->any())->method('getDefinitions')->will($this->returnValue($this->definitions));
 }
 /**
  * @covers ::isOrHasAncestor
  * @depends testGetDefinitions
  */
 public function testIsOrHasAncestor()
 {
     $definitions = array('foo' => array('id' => 'foo'), 'bar' => array('id' => 'bar', 'parent_id' => 'foo'), 'baz' => array('id' => 'baz', 'parent_id' => 'bar'));
     $this->discovery->expects($this->once())->method('getDefinitions')->willReturn($definitions);
     $this->assertTrue($this->sut->isOrHasAncestor('baz', 'foo'));
     $this->assertTrue($this->sut->isOrHasAncestor('baz', 'baz'));
 }
 /**
  * Sets up the entity manager to be tested.
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $definitions
  *   (optional) An array of entity type definitions.
  */
 protected function setUpEntityManager($definitions = array())
 {
     $class = $this->getMockClass(EntityInterface::class);
     foreach ($definitions as $key => $entity_type) {
         // \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called
         // by \Drupal\Core\Entity\EntityManager::processDefinition() so it must
         // always be mocked.
         $entity_type->getLinkTemplates()->willReturn([]);
         // Give the entity type a legitimate class to return.
         $entity_type->getClass()->willReturn($class);
         $definitions[$key] = $entity_type->reveal();
     }
     $this->discovery->getDefinition(Argument::cetera())->will(function ($args) use($definitions) {
         $entity_type_id = $args[0];
         $exception_on_invalid = $args[1];
         if (isset($definitions[$entity_type_id])) {
             return $definitions[$entity_type_id];
         } elseif (!$exception_on_invalid) {
             return NULL;
         } else {
             throw new PluginNotFoundException($entity_type_id);
         }
     });
     $this->discovery->getDefinitions()->willReturn($definitions);
 }
 /**
  * Tests the plugins alter hook.
  */
 public function testPluginDefinitionAlter()
 {
     $definitions['test_plugin'] = array('id' => 'test_plugin', 'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault', 'title' => 'Plugin', 'weight' => 2, 'group' => 'group1', 'route_name' => 'test_route', 'options' => array('key' => 'value'));
     $this->pluginDiscovery->expects($this->once())->method('getDefinitions')->will($this->returnValue($definitions));
     $this->moduleHandler->expects($this->once())->method('alter')->with('contextual_links_plugins', $definitions);
     $this->contextualLinkManager->getDefinition('test_plugin');
 }
Example #11
0
 /**
  * Tests getDefinitions() and getDefinition().
  */
 function testDiscoveryInterface()
 {
     // Ensure that getDefinitions() returns the expected definitions.
     // For the arrays to be identical (instead of only equal), they must be
     // sorted equally, which seems unnecessary here.
     // The discovered definitions may contain circular references; use a custom
     // assertion message to prevent var_export() from getting called.
     $this->assertEqual($this->discovery->getDefinitions(), $this->expectedDefinitions, 'Expected definitions found.');
     // Ensure that getDefinition() returns the expected definition.
     foreach ($this->expectedDefinitions as $id => $definition) {
         $this->assertDefinitionIdentical($this->discovery->getDefinition($id), $definition);
     }
     // Ensure that an empty array is returned if no plugin definitions are found.
     $this->assertIdentical($this->emptyDiscovery->getDefinitions(), array(), 'array() returned if no plugin definitions are found.');
     // Ensure that NULL is returned as the definition of a non-existing plugin.
     $this->assertIdentical($this->emptyDiscovery->getDefinition('non_existing', FALSE), NULL, 'NULL returned as the definition of a non-existing plugin.');
 }
 /**
  * @covers ::__construct
  * @covers ::createInstance
  *
  * @expectedException \Drupal\Component\Plugin\Exception\PluginNotFoundException
  */
 public function testCreateInstanceWithNonExistingPluginAndOverriddenDiscovery()
 {
     $plugin_manager = $this->getMock(PluginManagerInterface::class);
     $this->decoratedDiscovery = $this->getMock(DiscoveryInterface::class);
     $this->decoratedFactory = $plugin_manager;
     $this->sut = new PluginManagerDecorator($plugin_manager, $this->decoratedDiscovery);
     $plugin_id = $this->randomMachineName();
     $this->decoratedDiscovery->expects($this->once())->method('getDefinitions')->willReturn([]);
     $this->decoratedFactory->expects($this->never())->method('createInstance');
     $this->sut->createInstance($plugin_id);
 }
 /**
  * @covers ::getDefinitions
  */
 public function testGetDefinitions()
 {
     $plugin_id_a = $this->randomMachineName();
     $plugin_id_b = $this->randomMachineName();
     $plugin_id_c = $this->randomMachineName();
     $discovery_definitions = [$plugin_id_a => ['label' => $this->randomMachineName()], $plugin_id_b => ['label' => $this->randomMachineName(), 'description' => $this->randomMachineName()], $plugin_id_c => ['label' => $this->randomMachineName()]];
     $this->discovery->expects($this->atLeastOnce())->method('getDefinitions')->willReturn($discovery_definitions);
     $definitions = $this->sut->getDefinitions();
     foreach ([$plugin_id_a, $plugin_id_b, $plugin_id_c] as $plugin_id) {
         $this->assertArrayHasKey('description', $definitions[$plugin_id]);
     }
 }
 /**
  * Tests a single definition when a derivative already exists.
  */
 public function testSingleExistingDerivative()
 {
     $base_definition = array('id' => 'non_container_aware_discovery', 'deriver' => '\\Drupal\\Tests\\Core\\Plugin\\Discovery\\TestDerivativeDiscovery', 'string' => 'string', 'empty_string' => 'not_empty', 'array' => array('one', 'two'), 'empty_array' => array('three'), 'null_value' => TRUE);
     // This will clash with a derivative id.
     // @see \Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery
     $derivative_definition = array('id' => 'non_container_aware_discovery:test_discovery_1', 'string' => 'string', 'empty_string' => '', 'array' => array('one', 'two'), 'empty_array' => array(), 'null_value' => NULL);
     $this->discoveryMain->expects($this->at(0))->method('getDefinition')->with('non_container_aware_discovery:test_discovery_1')->will($this->returnValue($derivative_definition));
     $this->discoveryMain->expects($this->at(1))->method('getDefinition')->with('non_container_aware_discovery')->will($this->returnValue($base_definition));
     $discovery = new DerivativeDiscoveryDecorator($this->discoveryMain);
     $expected = $base_definition;
     $expected['id'] = 'non_container_aware_discovery:test_discovery_1';
     $this->assertArrayEquals($expected, $discovery->getDefinition('non_container_aware_discovery:test_discovery_1'));
 }
 /**
  * @covers \Drupal\Core\Menu\LocalActionManager::getActionsForRoute()
  *
  * @dataProvider getActionsForRouteProvider
  */
 public function testGetActionsForRoute($route_appears, array $plugin_definitions, array $expected_actions)
 {
     $this->discovery->expects($this->any())->method('getDefinitions')->will($this->returnValue($plugin_definitions));
     $map = array();
     foreach ($plugin_definitions as $plugin_id => $plugin_definition) {
         $plugin = $this->getMock('Drupal\\Core\\Menu\\LocalActionInterface');
         $plugin->expects($this->any())->method('getRouteName')->will($this->returnValue($plugin_definition['route_name']));
         $plugin->expects($this->any())->method('getRouteParameters')->will($this->returnValue(isset($plugin_definition['route_parameters']) ? $plugin_definition['route_parameters'] : array()));
         $plugin->expects($this->any())->method('getTitle')->will($this->returnValue($plugin_definition['title']));
         $this->controllerResolver->expects($this->any())->method('getArguments')->with($this->request, array($plugin, 'getTitle'))->will($this->returnValue(array()));
         $plugin->expects($this->any())->method('getWeight')->will($this->returnValue($plugin_definition['weight']));
         $this->controllerResolver->expects($this->any())->method('getArguments')->with($this->request, array($plugin, 'getTitle'))->will($this->returnValue(array()));
         $map[] = array($plugin_id, array(), $plugin);
     }
     $this->factory->expects($this->any())->method('createInstance')->will($this->returnValueMap($map));
     $this->assertEquals($expected_actions, $this->localActionManager->getActionsForRoute($route_appears));
 }
 /**
  * @covers ::useCaches
  */
 public function testUseCachesWithCachedDecoratedDiscovery()
 {
     $this->decoratedDiscovery = $this->getMockForAbstractClass(PluginDiscoveryDecoratorTestCachedDiscovery::class);
     $this->sut = new PluginDiscoveryDecorator($this->decoratedDiscovery);
     $this->decoratedDiscovery->expects($this->once())->method('clearCachedDefinitions');
     $this->decoratedDiscovery->expects($this->exactly(3))->method('getDefinitions')->willReturn([]);
     // There are no cached definitions yet, so this should call the decorated
     // discovery.
     $this->sut->getDefinitions();
     // This should return the cached definitions.
     $this->sut->getDefinitions();
     $this->sut->useCaches(FALSE);
     // This should return newly built definitions, so this should call the
     // decorated discovery.
     $this->sut->getDefinitions();
     // This should return newly built definitions again, because we disabled
     // caching.
     $this->sut->getDefinitions();
 }
 /**
  * Sets up the entity manager to be tested.
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface[]|\PHPUnit_Framework_MockObject_MockObject[] $definitions
  *   (optional) An array of entity type definitions.
  */
 protected function setUpEntityManager($definitions = array())
 {
     $class = $this->getMockClass('Drupal\\Core\\Entity\\EntityInterface');
     foreach ($definitions as $entity_type) {
         $entity_type->expects($this->any())->method('getClass')->will($this->returnValue($class));
     }
     $this->discovery->expects($this->any())->method('getDefinition')->will($this->returnCallback(function ($entity_type_id, $exception_on_invalid = FALSE) use($definitions) {
         if (isset($definitions[$entity_type_id])) {
             return $definitions[$entity_type_id];
         } elseif (!$exception_on_invalid) {
             return NULL;
         } else {
             throw new PluginNotFoundException($entity_type_id);
         }
     }));
     $this->discovery->expects($this->any())->method('getDefinitions')->will($this->returnValue($definitions));
     $this->entityManager = new TestEntityManager(new \ArrayObject(), $this->moduleHandler, $this->cacheBackend, $this->languageManager, $this->translationManager, $this->getClassResolverStub(), $this->typedDataManager, $this->installedDefinitions, $this->eventDispatcher);
     $this->entityManager->setContainer($this->container);
     $this->entityManager->setDiscovery($this->discovery);
 }
 /**
  * {@inheritdoc}
  */
 public function hasDefinition($plugin_id)
 {
     return (bool) $this->discovery->hasDefinition($plugin_id);
 }
 /**
  * Tests plugin manager's getDefinitions method.
  */
 public function testGetDefinitions()
 {
     $definitions = array('foo' => array('label' => $this->randomMachineName()));
     $this->discovery->expects($this->once())->method('getDefinitions')->willReturn($definitions);
     $this->assertSame($definitions, $this->sut->getDefinitions());
 }
 /**
  * {@inheritdoc}
  */
 public function createInstance($plugin_id, array $configuration = array())
 {
     $plugin_definition = $this->discovery->getDefinition($plugin_id);
     $plugin_class = static::getPluginClass($plugin_id, $plugin_definition, $this->interface);
     return new $plugin_class($configuration, $plugin_id, $plugin_definition);
 }
 /**
  * {@inheritdoc}
  */
 public function getDefinitions()
 {
     return $this->discovery->getDefinitions();
 }
Example #22
0
 /**
  * {@inheritdoc}
  */
 public function getDefinitions()
 {
     return parent::getDefinitions() + $this->decorated->getDefinitions();
 }
 /**
  * {@inheritdoc}
  *
  * @throws \Drupal\Component\Plugin\Exception\InvalidDeriverException
  *   Thrown if the 'deriver' class specified in the plugin definition
  *   does not implement \Drupal\Component\Plugin\Derivative\DeriverInterface.
  */
 public function getDefinitions()
 {
     $plugin_definitions = $this->decorated->getDefinitions();
     return $this->getDerivatives($plugin_definitions);
 }
 /**
  * {@inheritdoc}
  */
 public function getDefinitions()
 {
     return static::filterDefinitions($this->decorated->getDefinitions(), $this->providerExists);
 }