From aecc5a4a454c428c7621dc9845c395acd950bc74 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 27 Jul 2015 13:38:53 +0100 Subject: [PATCH] Issue #2465751 by andypost, JeroenT, RavindraSingh, tim.plunkett, Berdir: Remove config_test_load() and replace with entity_load --- .../Tests/ConfigEntityFormOverrideTest.php | 6 ++- .../tests/config_test/config_test.module | 10 ---- .../tests/config_test/src/ConfigTestForm.php | 51 ++++++++++++++++++- .../Tests/ConfigTranslationOverviewTest.php | 4 +- 4 files changed, 57 insertions(+), 14 deletions(-) diff --git a/core/modules/config/src/Tests/ConfigEntityFormOverrideTest.php b/core/modules/config/src/Tests/ConfigEntityFormOverrideTest.php index a07da59ed3c..632d2dcf146 100644 --- a/core/modules/config/src/Tests/ConfigEntityFormOverrideTest.php +++ b/core/modules/config/src/Tests/ConfigEntityFormOverrideTest.php @@ -31,6 +31,8 @@ class ConfigEntityFormOverrideTest extends WebTestBase { $overridden_label = 'Overridden label'; $edited_label = 'Edited label'; + $config_test_storage = $this->container->get('entity.manager')->getStorage('config_test'); + // Set up an override. $settings['config']['config_test.dynamic.dotted.default']['label'] = (object) array( 'value' => $overridden_label, @@ -39,7 +41,7 @@ class ConfigEntityFormOverrideTest extends WebTestBase { $this->writeSettings($settings); // Test that the overridden label is loaded with the entity. - $this->assertEqual(config_test_load('dotted.default')->label(), $overridden_label); + $this->assertEqual($config_test_storage->load('dotted.default')->label(), $overridden_label); // Test that the original label on the listing page is intact. $this->drupalGet('admin/structure/config_test'); @@ -67,7 +69,7 @@ class ConfigEntityFormOverrideTest extends WebTestBase { $this->assertIdentical((string) $elements[0]['value'], $edited_label); // Test that the overridden label is still loaded with the entity. - $this->assertEqual(config_test_load('dotted.default')->label(), $overridden_label); + $this->assertEqual($config_test_storage->load('dotted.default')->label(), $overridden_label); } } diff --git a/core/modules/config/tests/config_test/config_test.module b/core/modules/config/tests/config_test/config_test.module index c17d688f4ed..9510abab67a 100644 --- a/core/modules/config/tests/config_test/config_test.module +++ b/core/modules/config/tests/config_test/config_test.module @@ -10,16 +10,6 @@ use Symfony\Component\HttpFoundation\RedirectResponse; require_once dirname(__FILE__) . '/config_test.hooks.inc'; -/** - * Loads a ConfigTest object. - * - * @param string $id - * The ID of the ConfigTest object to load. - */ -function config_test_load($id) { - return entity_load('config_test', $id); -} - /** * Implements hook_cache_flush(). */ diff --git a/core/modules/config/tests/config_test/src/ConfigTestForm.php b/core/modules/config/tests/config_test/src/ConfigTestForm.php index 0967206c301..ac0bb17ae8f 100644 --- a/core/modules/config/tests/config_test/src/ConfigTestForm.php +++ b/core/modules/config/tests/config_test/src/ConfigTestForm.php @@ -8,13 +8,41 @@ namespace Drupal\config_test; use Drupal\Core\Entity\EntityForm; +use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Form\FormStateInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Form controller for the test config edit forms. */ class ConfigTestForm extends EntityForm { + /** + * The entity query. + * + * @var \Drupal\Core\Entity\Query\QueryFactory + */ + protected $entityQuery; + + /** + * Constructs a new ConfigTestForm. + * + * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query + * The entity query. + */ + public function __construct(QueryFactory $entity_query) { + $this->entityQuery = $entity_query; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.query') + ); + } + /** * {@inheritdoc} */ @@ -33,7 +61,7 @@ class ConfigTestForm extends EntityForm { '#default_value' => $entity->id(), '#required' => TRUE, '#machine_name' => array( - 'exists' => 'config_test_load', + 'exists' => [$this, 'exists'], 'replace_pattern' => '[^a-z0-9_.]+', ), ); @@ -142,4 +170,25 @@ class ConfigTestForm extends EntityForm { $form_state->setRedirectUrl($this->entity->urlInfo('collection')); } + /** + * Determines if the entity already exists. + * + * @param string|int $entity_id + * The entity ID. + * @param array $element + * The form element. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @return bool + * TRUE if the entity exists, FALSE otherwise. + */ + public function exists($entity_id, array $element, FormStateInterface $form_state) { + /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */ + $entity = $form_state->getFormObject()->getEntity(); + return (bool) $this->entityQuery->get($entity->getEntityTypeId()) + ->condition($entity->getEntityType()->getKey('id'), $entity_id) + ->execute(); + } + } diff --git a/core/modules/config_translation/src/Tests/ConfigTranslationOverviewTest.php b/core/modules/config_translation/src/Tests/ConfigTranslationOverviewTest.php index 4c83a38133e..0916dcd7959 100644 --- a/core/modules/config_translation/src/Tests/ConfigTranslationOverviewTest.php +++ b/core/modules/config_translation/src/Tests/ConfigTranslationOverviewTest.php @@ -153,6 +153,8 @@ class ConfigTranslationOverviewTest extends WebTestBase { $original_label = 'Default'; $overridden_label = 'Overridden label'; + $config_test_storage = $this->container->get('entity.manager')->getStorage('config_test'); + // Set up an override. $settings['config']['config_test.dynamic.dotted.default']['label'] = (object) array( 'value' => $overridden_label, @@ -161,7 +163,7 @@ class ConfigTranslationOverviewTest extends WebTestBase { $this->writeSettings($settings); // Test that the overridden label is loaded with the entity. - $this->assertEqual(config_test_load('dotted.default')->label(), $overridden_label); + $this->assertEqual($config_test_storage->load('dotted.default')->label(), $overridden_label); // Test that the original label on the listing page is intact. $this->drupalGet('admin/config/regional/config-translation/config_test');