/** * Tests storage methods. */ public function testStorageMethods() { $entity_type = \Drupal::entityManager()->getDefinition('config_test'); // Test the static extractID() method. $expected_id = 'test_id'; $config_name = $entity_type->getConfigPrefix() . '.' . $expected_id; $storage = $this->storage; $this->assertIdentical($storage::getIDFromConfigName($config_name, $entity_type->getConfigPrefix()), $expected_id); // Create three entities, two with the same style. $style = $this->randomMachineName(8); for ($i = 0; $i < 2; $i++) { $entity = $this->storage->create(array('id' => $this->randomMachineName(), 'label' => $this->randomString(), 'style' => $style)); $entity->save(); } $entity = $this->storage->create(array('id' => $this->randomMachineName(), 'label' => $this->randomString(), 'style' => $this->randomMachineName(9))); $entity->save(); // Ensure that the configuration entity can be loaded by UUID. $entity_loaded_by_uuid = entity_load_by_uuid($entity_type->id(), $entity->uuid()); if (!$entity_loaded_by_uuid) { $this->fail(sprintf("Failed to load '%s' entity ID '%s' by UUID '%s'.", $entity_type->id(), $entity->id(), $entity->uuid())); } // Compare UUIDs as the objects are not identical since // $entity->enforceIsNew is FALSE and $entity_loaded_by_uuid->enforceIsNew // is NULL. $this->assertIdentical($entity->uuid(), $entity_loaded_by_uuid->uuid()); $entities = $this->storage->loadByProperties(); $this->assertEqual(count($entities), 3, 'Three entities are loaded when no properties are specified.'); $entities = $this->storage->loadByProperties(array('style' => $style)); $this->assertEqual(count($entities), 2, 'Two entities are loaded when the style property is specified.'); // Assert that both returned entities have a matching style property. foreach ($entities as $entity) { $this->assertIdentical($entity->get('style'), $style, 'The loaded entity has the correct style value specified.'); } }
/** * Render form label. */ protected function renderName($form_id, $values) { if ($form_id !== NULL && $form_id !== '') { $type = $this->formStorage->load($form_id); return $type ? $this->sanitizeValue($type->label()) : ''; } return $this->sanitizeValue($form_id); }
/** * {@inheritdoc} */ public function getAll($scope = NULL) { if ($scope) { $zones = $this->zoneStorage->loadByProperties(['scope' => $scope]); } else { $zones = $this->zoneStorage->loadMultiple(); } return $zones; }
/** * {@inheritdoc} */ public function getAll($locale = NULL) { if ($locale) { $original_language = $this->languageManager->getConfigOverrideLanguage(); $this->languageManager->setConfigOverrideLanguage(new Language(['id' => $locale])); $address_formats = $this->formatStorage->loadMultiple(); $this->languageManager->setConfigOverrideLanguage($original_language); } else { $address_formats = $this->formatStorage->loadMultiple(); } return $address_formats; }
/** * {@inheritdoc} */ public function importTranslations(array $langcodes) { $available_translations = $this->getAvailableTranslations(); $available_translations = array_intersect_key($available_translations, array_flip($langcodes)); foreach ($available_translations as $langcode => $country_codes) { $address_formats = $this->storage->loadMultiple($country_codes); foreach ($address_formats as $country_code => $address_format) { $external_translation = $this->externalRepository->get($country_code, $langcode); $config_name = $address_format->getConfigDependencyName(); $config_translation = $this->languageManager->getLanguageConfigOverride($langcode, $config_name); $config_translation->set('format', $external_translation->getFormat()); $config_translation->save(); } } }
/** * Tests overriding an existing route with configured parameters. * * @covers ::alterRoutes * @covers ::findPageRouteName * * @dataProvider providerTestAlterRoutesOverrideExisting */ public function testAlterRoutesOverrideExistingWithConfiguredParameters($page_path, $existing_route_path, $requirements = []) { $route_name = 'test_route'; // Set up a page with the same path as an existing route. /** @var \Drupal\page_manager\PageInterface|\Prophecy\Prophecy\ProphecyInterface $page */ $page = $this->prophesize(PageInterface::class); $page->status()->willReturn(TRUE); $page->getPath()->willReturn($page_path); $page->id()->willReturn('page1'); $page->label()->willReturn(NULL); $page->usesAdminTheme()->willReturn(FALSE); $page->getParameters()->willReturn(['foo' => ['machine_name' => 'foo', 'type' => 'integer', 'label' => 'Foo'], 'test_route' => ['machine_name' => 'test_route', 'type' => '', 'label' => '']]); $variant1 = $this->prophesize(PageVariantInterface::class); $variant1->getWeight()->willReturn(0); $page->getVariants()->willReturn(['variant1' => $variant1->reveal()]); $this->pageStorage->loadMultiple()->willReturn(['page1' => $page->reveal()]); $collection = new RouteCollection(); $collection->add($route_name, new Route($existing_route_path, ['default_exists' => 'default_value'], $requirements, ['parameters' => ['foo' => ['bar' => 'bar']]])); $route_event = new RouteBuildEvent($collection); $this->routeSubscriber->onAlterRoutes($route_event); $expected_defaults = ['_entity_view' => 'page_manager_page_variant', '_title' => NULL, 'page_manager_page_variant' => 'variant1', 'page_manager_page' => 'page1', 'page_manager_page_variant_weight' => 0, 'base_route_name' => $route_name]; $expected_requirements = $requirements + ['_page_access' => 'page_manager_page.view']; $expected_options = ['compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler', 'parameters' => ['foo' => ['bar' => 'bar', 'type' => 'integer'], 'page_manager_page_variant' => ['type' => 'entity:page_variant'], 'page_manager_page' => ['type' => 'entity:page']], '_admin_route' => FALSE]; $this->assertMatchingRoute($collection->get($route_name), $existing_route_path, $expected_defaults, $expected_requirements, $expected_options); }
/** * Tests overriding an existing route. * * @covers ::alterRoutes * @covers ::findPageRouteName * * @dataProvider providerTestAlterRoutesOverrideExisting */ public function testAlterRoutesOverrideExisting($page_path, $existing_route_path, $requirements = []) { $route_name = 'test_route'; // Set up a page with the same path as an existing route. $page = $this->prophesize(PageInterface::class); $page->status()->willReturn(TRUE)->shouldBeCalled(); $page->getPath()->willReturn($page_path)->shouldBeCalled(); $variant1 = $this->prophesize(PageVariantInterface::class); $variant1->getWeight()->willReturn(0); $page->getVariants()->willReturn(['variant1' => $variant1->reveal()]); $page->id()->willReturn('page1'); $page->label()->willReturn(NULL); $page->usesAdminTheme()->willReturn(FALSE); $this->pageStorage->loadMultiple()->willReturn(['page1' => $page->reveal()])->shouldBeCalledTimes(1); $this->cacheTagsInvalidator->invalidateTags(["page_manager_route_name:{$route_name}"])->shouldBeCalledTimes(1); $collection = new RouteCollection(); $collection->add($route_name, new Route($existing_route_path, ['default_exists' => 'default_value'], $requirements, ['parameters' => ['foo' => 'bar']])); $route_event = new RouteBuildEvent($collection); $this->routeSubscriber->onAlterRoutes($route_event); // The normal route name is not used, the existing route name is instead. $this->assertSame(1, $collection->count()); $this->assertNull($collection->get('page_manager.page_view_page1')); $this->assertNull($collection->get('page_manager.page_view_page1_variant1')); $route = $collection->get($route_name); $expected_defaults = ['_entity_view' => 'page_manager_page_variant', '_title' => NULL, 'page_manager_page_variant' => 'variant1', 'page_manager_page' => 'page1', 'page_manager_page_variant_weight' => 0, 'base_route_name' => $route_name]; $expected_requirements = $requirements; $expected_options = ['compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler', 'parameters' => ['page_manager_page_variant' => ['type' => 'entity:page_variant'], 'page_manager_page' => ['type' => 'entity:page'], 'foo' => 'bar'], '_admin_route' => FALSE]; $this->assertMatchingRoute($route, $existing_route_path, $expected_defaults, $expected_requirements, $expected_options); }
/** * Sets allow_existing IEF setting. * * @param bool $flag * "allow_existing" flag to be set. */ protected function setAllowExisting($flag) { /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */ $display = $this->entityFormDisplayStorage->load('node.ief_test_complex.default'); $component = $display->getComponent('multi'); $component['settings']['allow_existing'] = $flag; $display->setComponent('multi', $component)->save(); }
/** * {@inheritdoc} */ public function setUp() { $this->entityTypeId = $this->randomName(); $this->provider = $this->randomName(); $this->entityType = $this->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface'); $this->entityType->expects($this->any())->method('getProvider')->will($this->returnValue($this->provider)); $this->entityManager = $this->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface'); $this->entityManager->expects($this->any())->method('getDefinition')->with($this->entityTypeId)->will($this->returnValue($this->entityType)); $this->uuid = $this->getMock('\\Drupal\\Component\\Uuid\\UuidInterface'); $this->breakpointGroupId = $this->randomName(9); $this->breakpointGroup = $this->getMock('Drupal\\breakpoint\\Entity\\BreakpointGroup', array(), array(array('name' => 'test', 'id' => $this->breakpointGroupId))); $this->breakpointGroupStorage = $this->getMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface'); $this->breakpointGroupStorage->expects($this->any())->method('load')->with($this->breakpointGroupId)->will($this->returnValue($this->breakpointGroup)); $this->entityManager->expects($this->any())->method('getStorage')->will($this->returnValue($this->breakpointGroupStorage)); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); $container->set('uuid', $this->uuid); \Drupal::setContainer($container); }
/** * Tests block render cache handling. */ public function testBlockViewBuilderCache() { // Verify cache handling for a non-empty block. $this->verifyRenderCacheHandling(); // Create an empty block. $this->block = $this->controller->create(array('id' => 'test_block', 'theme' => 'stark', 'plugin' => 'test_cache')); $this->block->save(); \Drupal::state()->set('block_test.content', NULL); // Verify cache handling for an empty block. $this->verifyRenderCacheHandling(); }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); // The machine name field should already check to see if the requested // machine name is available. $pattern = trim($form_state->getValue('date_format_pattern')); foreach ($this->dateFormatStorage->loadMultiple() as $format) { if ($format->getPattern() == $pattern && $format->id() == $this->entity->id()) { drupal_set_message(t('The existing format/name combination has not been altered.')); continue; } } }
/** * {@inheritdoc} */ public function validate(array $form, FormStateInterface $form_state) { parent::validate($form, $form_state); // The machine name field should already check to see if the requested // machine name is available. Regardless of machine_name or human readable // name, check to see if the provided pattern exists. $pattern = trim($form_state->getValue('date_format_pattern')); foreach ($this->dateFormatStorage->loadMultiple() as $format) { if ($format->getPattern() == $pattern && ($this->entity->isNew() || $format->id() != $this->entity->id())) { $form_state->setErrorByName('date_format_pattern', $this->t('This format already exists. Enter a unique format string.')); continue; } } }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $options = []; foreach ($this->typeStorage->loadMultiple() as $type) { $options[$type->id()] = $type->label(); } $form['crop_type'] = [ '#type' => 'select', '#title' => t('Crop type'), '#default_value' => $this->configuration['crop_type'], '#options' => $options, '#description' => t('Crop type to be used for the image style.'), ]; return $form; }
/** * @covers ::render * * @depends testBuildHeader */ public function testRender() { $query = $this->getMock(QueryInterface::class); $query->expects($this->atLeastOnce())->method('pager')->willReturnSelf(); $query->expects($this->atLeastOnce())->method('sort')->willReturnSelf(); $this->entityStorage->expects($this->atLeastOnce())->method('getQuery')->willReturn($query); $this->entityType->expects($this->any())->method('getClass')->willReturn(ConfigEntityBase::class); $this->entityStorage->expects($this->once())->method('loadMultipleOverrideFree')->willReturn([]); $build = $this->sut->render(); unset($build['table']['#attached']); unset($build['table']['#header']); $expected_build = array('#type' => 'table', '#title' => NULL, '#rows' => [], '#attributes' => array('class' => array('payment-method-configuration-list')), '#cache' => ['contexts' => NULL, 'tags' => NULL]); $this->assertInstanceOf(TranslatableMarkup::class, $build['table']['#empty']); unset($build['table']['#empty']); $this->assertEquals($expected_build, $build['table']); }
/** * Tests the sortSearchPages() method. */ public function testSortSearchPages() { $entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface'); $entity_type->expects($this->any())->method('getClass')->will($this->returnValue('Drupal\\Tests\\search\\Unit\\TestSearchPage')); $this->storage->expects($this->once())->method('getEntityType')->will($this->returnValue($entity_type)); // Declare entities out of their expected order so we can be sure they were // sorted. We cannot mock these because of uasort(), see // https://bugs.php.net/bug.php?id=50688. $unsorted_entities['test4'] = new TestSearchPage(array('weight' => 0, 'status' => FALSE, 'label' => 'Test4')); $unsorted_entities['test3'] = new TestSearchPage(array('weight' => 10, 'status' => TRUE, 'label' => 'Test3')); $unsorted_entities['test2'] = new TestSearchPage(array('weight' => 0, 'status' => TRUE, 'label' => 'Test2')); $unsorted_entities['test1'] = new TestSearchPage(array('weight' => 0, 'status' => TRUE, 'label' => 'Test1')); $expected = $unsorted_entities; ksort($expected); $sorted_entities = $this->searchPageRepository->sortSearchPages($unsorted_entities); $this->assertSame($expected, $sorted_entities); }
/** * Set up the ief_test_nested1 node add form. * * Sets the nested fields' required settings. * Gets the form. * Opens the inline entity forms if they are not required. * * @param boolean $required * Whether the fields are required. */ protected function setupNestedComplexForm($required) { /** @var \Drupal\Core\Field\FieldConfigInterface $ief_test_nested1 */ $ief_test_nested1 = $this->fieldConfigStorage->load('node.ief_test_nested1.test_ref_nested1'); $ief_test_nested1->setRequired($required); $ief_test_nested1->save(); /** @var \Drupal\Core\Field\FieldConfigInterface $ief_test_nested2 */ $ief_test_nested2 = $this->fieldConfigStorage->load('node.ief_test_nested2.test_ref_nested2'); $ief_test_nested2->setRequired($required); $ief_test_nested2->save(); $this->drupalGet('node/add/ief_test_nested1'); if (!$required) { // Open inline forms if not required. $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Add new node 2"]')); $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Add new node 3"]')); } }
/** * @covers ::filter */ public function testFilterRequestAttributes() { $route_collection = new RouteCollection(); $request = new Request([], [], ['foo' => 'bar', 'slug' => 2]); $route = new Route('/path/with/{slug}', ['page_manager_page_variant' => 'a_variant']); $route_collection->add('a_route', $route); $page_variant = $this->prophesize(PageVariantInterface::class); $page_variant->access('view')->willReturn(TRUE); $this->currentPath->getPath($request)->willReturn('/path/with/1'); $this->pageVariantStorage->load('a_variant')->willReturn($page_variant->reveal()); $route_enhancer = $this->prophesize(RouteEnhancerInterface::class); $this->routeFilter->addRouteEnhancer($route_enhancer->reveal()); $result_enhance_attributes = $expected_enhance_attributes = ['foo' => 'bar', 'slug' => '1', 'page_manager_page_variant' => 'a_variant', '_route_object' => $route, '_route' => 'a_route']; $result_enhance_attributes['slug'] = 'slug 1'; $route_enhancer->enhance($expected_enhance_attributes, $request)->willReturn($result_enhance_attributes); $result = $this->routeFilter->filter($route_collection, $request); $expected = ['a_route' => $route]; $this->assertSame($expected, $result->all()); $expected_attributes = ['foo' => 'bar', 'slug' => 'slug 1', 'page_manager_page_variant' => 'a_variant', '_route_object' => $route, '_route' => 'a_route']; $this->assertSame($expected_attributes, $request->attributes->all()); }
/** * @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage * @param string $config_name * * @return string */ protected function getEntityId(ConfigEntityStorageInterface $entity_storage, $config_name) { // getIDFromConfigName adds a dot but getConfigPrefix has a dot already. return $entity_storage::getIDFromConfigName($config_name, $entity_storage->getEntityType()->getConfigPrefix()); }
/** * Tests whether deleting a server works correctly. * * @param \Drupal\search_api\ServerInterface $server * The server used for this test. */ public function serverDelete(ServerInterface $server) { $this->storage->delete(array($server)); $loaded_server = $this->storage->load($server->id()); $this->assertNull($loaded_server); }
/** * Returns all enabled filter formats. * * @return \Drupal\filter\FilterFormatInterface[] */ protected function getEnabledFilterFormats() { return $this->filterStorage->loadByProperties(['status' => TRUE]); }
/** * Tests whether deleting an index works correctly. * * @param \Drupal\search_api\IndexInterface $index * The index used for the test. */ public function indexDelete(IndexInterface $index) { $this->storage->delete(array($index)); $loaded_index = $this->storage->load($index->id()); $this->assertNull($loaded_index); }
/** * Returns all field storages for a specified module. * * @param string $module * The module to filter field storages by. * * @return \Drupal\field\FieldStorageConfigInterface[] * An array of field storages for a specified module. */ protected function getFieldStoragesByModule($module) { return $this->fieldStorageConfigStorage->loadByProperties(['module' => $module, 'include_deleted' => TRUE]); }
/** * Returns the vocabulary configured for forums. * * @return \Drupal\taxonomy\VocabularyInterface * The vocabulary entity for forums. */ protected function getForumVocabulary() { $vid = $this->configFactory->get('forum.settings')->get('vocabulary'); return $this->vocabularyStorage->load($vid); }
/** * Tests overriding an existing route. * * @covers ::alterRoutes */ public function testAlterRoutesOverrideExisting() { // Set up a page with the same path as an existing route. $page = $this->prophesize(PageInterface::class); $page->status()->willReturn(TRUE)->shouldBeCalledTimes(1); $page->getPath()->willReturn('/test_route')->shouldBeCalledTimes(1); $page->isFallbackPage()->willReturn(FALSE); $page->label()->willReturn(NULL); $page->usesAdminTheme()->willReturn(FALSE); $this->pageStorage->loadMultiple()->willReturn(['page1' => $page->reveal()])->shouldBeCalledTimes(1); $collection = new RouteCollection(); $collection->add('test_route', new Route('test_route', [], [], ['parameters' => ['foo' => 'bar']])); $route_event = new RouteBuildEvent($collection); $this->routeSubscriber->onAlterRoutes($route_event); // The normal route name is not used, the existing route name is instead. $this->assertSame(1, $collection->count()); $this->assertNull($collection->get('page_manager.page_view_page1')); $route = $collection->get('test_route'); $expected_defaults = ['_entity_view' => 'page_manager_page', 'page_manager_page' => 'page1', '_title' => NULL]; $expected_requirements = ['_entity_access' => 'page_manager_page.view']; $expected_options = ['compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler', 'parameters' => ['page_manager_page' => ['type' => 'entity:page'], 'foo' => 'bar'], '_admin_route' => FALSE]; $this->assertMatchingRoute($route, '/test_route', $expected_defaults, $expected_requirements, $expected_options); }