diff --git a/config/install/nodeorder.settings.yml b/config/install/nodeorder.settings.yml index 548dfa973995dca0120b46adb303cc7c65ed8ba3..cb3ba9b173d7cfe505a8cc117156adacc2196307 100644 --- a/config/install/nodeorder.settings.yml +++ b/config/install/nodeorder.settings.yml @@ -1,4 +1,4 @@ -vocabularies: [] +vocabularies: { } override_taxonomy_page: true show_links_on_node: true link_to_ordering_page: true diff --git a/nodeorder.install b/nodeorder.install index 6803f521e6721876ee7833691abedbfee6d37d83..9a1c83fb175f513c00ad7fdc5e0e7e62dfbea294 100644 --- a/nodeorder.install +++ b/nodeorder.install @@ -5,6 +5,8 @@ * Nodeorder install file. */ +use \Drupal\nodeorder\ConfigManagerInterface; + /** * Implements hook_install(). * @@ -45,3 +47,19 @@ function nodeorder_uninstall() { $schema->dropIndex('taxonomy_index', 'weight'); $schema->dropField('taxonomy_index', 'weight'); } + +/** + * Update configs for migration to 2.x. + */ +function nodeorder_update_9001(&$sandbox) { + /** @var \Drupal\nodeorder\ConfigManagerInterface $configManager */ + $configManager = \Drupal::service('nodeorder.config_manager'); + $config = $configManager->config(); + + $vocabularies = array_filter($config->get(ConfigManagerInterface::KEY_VOCABULARIES)); + $vocabularies = array_keys($vocabularies); + + $configManager->updateConfigValues([ + ConfigManagerInterface::KEY_VOCABULARIES => array_combine($vocabularies, $vocabularies), + ]); +} diff --git a/nodeorder.module b/nodeorder.module index 5fe4cee2040a63637bd2b246a99ac1b3e155049e..8acbf7a26186feba76c63710c1a42b0fac588443 100755 --- a/nodeorder.module +++ b/nodeorder.module @@ -6,11 +6,12 @@ */ use Drupal\Core\Cache\Cache; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; use Drupal\node\NodeInterface; -use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\nodeorder\ConfigManagerInterface; use Drupal\taxonomy\Entity\Term; /** @@ -87,13 +88,16 @@ function nodeorder_form_taxonomy_vocabulary_form_alter(&$form, FormStateInterfac function nodeorder_taxonomy_vocabulary_form_submit($form, FormStateInterface $form_state) { /** @var \Drupal\nodeorder\NodeOrderManagerInterface $nodeorder_manager */ $nodeorder_manager = \Drupal::service('nodeorder.manager'); + /** @var \Drupal\nodeorder\ConfigManagerInterface $configManager */ + $configManager = \Drupal::service('nodeorder.config_manager'); + $config = $configManager->config(); $vocabulary = $form_state->getFormObject()->getEntity(); - $config = \Drupal::service('config.nodeorder_settings'); - $orderable = $form_state->getValue('orderable'); - $vocabularies = $config->get('vocabularies'); $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); + $orderable = $form_state->getValue('orderable'); + $vocabularies = $config->get(ConfigManagerInterface::KEY_VOCABULARIES); + if ($orderable && empty($vocabularies[$vocabulary->id()])) { // Switching from non-orderable to orderable. Cache::invalidateTags(['nodeorder']); @@ -156,9 +160,7 @@ function nodeorder_taxonomy_vocabulary_form_submit($form, FormStateInterface $fo } // Update config. - $vocabularies[$vocabulary->id()] = $orderable; - $config->set('vocabularies', $vocabularies); - $config->save(); + $configManager->updateOrderableValue($vocabulary->id(), $orderable); } /** diff --git a/nodeorder.routing.yml b/nodeorder.routing.yml index 79ef9c65976bb63b3baf55932e2f59c1bbb4435b..6ff250da737e5d4328a09fd44a43a95d6bf5a8ce 100644 --- a/nodeorder.routing.yml +++ b/nodeorder.routing.yml @@ -9,7 +9,6 @@ nodeorder.admin: _admin_route: TRUE nodeorder.admin_order: - parent: entity.taxonomy_term.canonical path: '/taxonomy/term/{taxonomy_term}/order' defaults: _controller: '\Drupal\nodeorder\Controller\NodeOrderListController::listing' diff --git a/nodeorder.services.yml b/nodeorder.services.yml index 243a76b26b81fd730641c585fadf5538c528025b..2a7c34cc3e33cddf62276e641bf9beafefaa72f6 100755 --- a/nodeorder.services.yml +++ b/nodeorder.services.yml @@ -1,4 +1,5 @@ services: + # @deprecated. use @nodeorder.config_manager instead. config.nodeorder_settings: class: Drupal\Core\Config\Config factory: config.factory:getEditable @@ -6,4 +7,8 @@ services: nodeorder.manager: class: Drupal\nodeorder\NodeOrderManager - arguments: ['@config.factory', '@database', '@entity_type.manager', '@cache.default'] + arguments: ['@nodeorder.config_manager', '@database', '@entity_type.manager', '@cache.default'] + + nodeorder.config_manager: + class: Drupal\nodeorder\ConfigManager + arguments: ['@config.factory'] diff --git a/src/ConfigManager.php b/src/ConfigManager.php new file mode 100644 index 0000000000000000000000000000000000000000..7a88a1f220c4af03dd042433c96343e027388f00 --- /dev/null +++ b/src/ConfigManager.php @@ -0,0 +1,56 @@ +<?php + +namespace Drupal\nodeorder; + +use Drupal\Core\Config\ConfigFactoryInterface; + +/** + * Defines a service that manage nodeorder configs. + */ +class ConfigManager implements ConfigManagerInterface { + + /** + * The configuration factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + public function __construct(ConfigFactoryInterface $config_factory) { + $this->configFactory = $config_factory; + } + + /** + * {@inheritdoc} + */ + public function config() { + // During a single PHP process the values can be changed many times, so it is important to get the actual values. + return $this->configFactory->getEditable(ConfigManagerInterface::CONFIG_NAME); + } + + public function updateOrderableValue(string $vocabularyId, bool $isOrderable) { + $config = $this->config(); + + $vocabularies = $config->get(ConfigManagerInterface::KEY_VOCABULARIES); + if ($isOrderable) { + $vocabularies[$vocabularyId] = $vocabularyId; + } else { + unset($vocabularies[$vocabularyId]); + } + + $config->set(ConfigManagerInterface::KEY_VOCABULARIES, $vocabularies); + $config->save(); + } + + public function updateConfigValues(array $data) { + $config = $this->config(); + + foreach (ConfigManagerInterface::CONFIG_KEYS as $key) { + if (isset($data[$key])) { + $config->set($key, $data[$key]); + } + } + + $config->save(); + } +} diff --git a/src/ConfigManagerInterface.php b/src/ConfigManagerInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..0789ea8a6f70c740bc5713a7a9ba2ec183ab17bd --- /dev/null +++ b/src/ConfigManagerInterface.php @@ -0,0 +1,33 @@ +<?php + +namespace Drupal\nodeorder; + +/** + * Provides an interface defining a ConfigManager. + */ +interface ConfigManagerInterface { + public const CONFIG_NAME = 'nodeorder.settings'; + public const KEY_SHOW_LINKS_ON_NODE = 'show_links_on_node'; + public const KEY_LINK_TO_ORDERING_PAGE = 'link_to_ordering_page'; + public const KEY_LINK_TO_ORDERING_PAGE_TAXONOMY_ADMIN = 'link_to_ordering_page_taxonomy_admin'; + public const KEY_OVERRIDE_TAXONOMY_PAGE = 'override_taxonomy_page'; + public const KEY_VOCABULARIES = 'vocabularies'; + public const CONFIG_KEYS = [ + self::KEY_SHOW_LINKS_ON_NODE, + self::KEY_LINK_TO_ORDERING_PAGE, + self::KEY_LINK_TO_ORDERING_PAGE_TAXONOMY_ADMIN, + self::KEY_OVERRIDE_TAXONOMY_PAGE, + self::KEY_VOCABULARIES, + ]; + + /** + * Retrieve 'nodeorder.settings' configuration object. + * + * @return \Drupal\Core\Config\Config + */ + public function config(); + + public function updateOrderableValue(string $vocabularyId, bool $isOrderable); + + public function updateConfigValues(array $data); +} diff --git a/src/Form/NodeorderAdminForm.php b/src/Form/NodeorderAdminForm.php index 643bd7f8d661b0a7802e184bcff25089c5ffa12e..ad12f1512d22806d4c4a8bba46f0b05644955217 100644 --- a/src/Form/NodeorderAdminForm.php +++ b/src/Form/NodeorderAdminForm.php @@ -6,6 +6,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\nodeorder\ConfigManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -20,6 +21,13 @@ class NodeorderAdminForm extends ConfigFormBase { */ protected $entityTypeBundleInfo; + /** + * The nodeorder config manager. + * + * @var \Drupal\nodeorder\ConfigManagerInterface + */ + protected $configManager; + /** * Constructs a NodeorderAdminForm object. * @@ -30,10 +38,12 @@ class NodeorderAdminForm extends ConfigFormBase { */ public function __construct( ConfigFactoryInterface $config_factory, - EntityTypeBundleInfoInterface $entity_type_bundle_info + EntityTypeBundleInfoInterface $entity_type_bundle_info, + ConfigManagerInterface $config_manager, ) { parent::__construct($config_factory); $this->entityTypeBundleInfo = $entity_type_bundle_info; + $this->configManager = $config_manager; } /** @@ -42,7 +52,8 @@ class NodeorderAdminForm extends ConfigFormBase { public static function create(ContainerInterface $container) { return new static( $container->get('config.factory'), - $container->get('entity_type.bundle.info') + $container->get('entity_type.bundle.info'), + $container->get('nodeorder.config_manager'), ); } @@ -57,7 +68,8 @@ class NodeorderAdminForm extends ConfigFormBase { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { - $config = $this->configFactory()->getEditable('nodeorder.settings'); + $config = $this->configManager->config(); + $form['nodeorder_show_links'] = [ '#type' => 'fieldset', '#title' => $this->t('Display ordering links'), @@ -66,7 +78,7 @@ class NodeorderAdminForm extends ConfigFormBase { $form['nodeorder_show_links']['nodeorder_show_links_on_node'] = [ '#type' => 'radios', '#title' => $this->t('Choose how to display ordering links'), - '#default_value' => $config->get('show_links_on_node'), + '#default_value' => $config->get(ConfigManagerInterface::KEY_SHOW_LINKS_ON_NODE), '#description' => $this->t('When displaying links based on the context, they will only be shown on taxonomy and nodeorder pages.'), '#options' => [ $this->t("Don't display ordering links"), @@ -79,7 +91,7 @@ class NodeorderAdminForm extends ConfigFormBase { $form['nodeorder_vocabularies'] = [ '#title' => $this->t('Vocabularies'), '#description' => $this->t('Select vocabularies for which the order can be changed.'), - '#default_value' => $config->get('vocabularies'), + '#default_value' => $config->get(ConfigManagerInterface::KEY_VOCABULARIES), '#type' => 'checkboxes', '#options' => array_combine(array_keys($bundles), array_column($bundles, 'label')), ]; @@ -88,21 +100,21 @@ class NodeorderAdminForm extends ConfigFormBase { '#type' => 'checkbox', '#title' => $this->t('Display link to the ordering page'), '#description' => $this->t('If enabled, a tab will appear on all <em>nodeorder/term/%</em> and <em>taxonomy/term/%</em> pages that quickly allows administrators to get to the node ordering administration page for the term.'), - '#default_value' => $config->get('link_to_ordering_page'), + '#default_value' => $config->get(ConfigManagerInterface::KEY_LINK_TO_ORDERING_PAGE), ]; $form['nodeorder_link_to_ordering_page_taxonomy_admin'] = [ '#type' => 'checkbox', '#title' => $this->t('Display link to the ordering page on taxonomy administration page'), '#description' => $this->t('If enabled, a tab will appear on <em>admin/content/taxonomy/%</em> pages that quickly allows administrators to get to the node ordering administration page for the term.'), - '#default_value' => $config->get('link_to_ordering_page_taxonomy_admin'), + '#default_value' => $config->get(ConfigManagerInterface::KEY_LINK_TO_ORDERING_PAGE_TAXONOMY_ADMIN), ]; $form['nodeorder_override_taxonomy_page'] = [ '#type' => 'checkbox', '#title' => $this->t('Override the default taxonomy page with one from nodeorder'), '#description' => $this->t('Disabling this will allow the panels module to override taxonomy pages instead. See <a href="https://www.drupal.org/node/1713048">this issue</a> for more information. You will have to clear caches for the change to take effect.'), - '#default_value' => $config->get('override_taxonomy_page'), + '#default_value' => $config->get(ConfigManagerInterface::KEY_OVERRIDE_TAXONOMY_PAGE), ]; return parent::buildForm($form, $form_state); @@ -119,13 +131,17 @@ class NodeorderAdminForm extends ConfigFormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $this->configFactory()->getEditable('nodeorder.settings') - ->set('show_links_on_node', $form_state->getValue('nodeorder_show_links_on_node')) - ->set('link_to_ordering_page', $form_state->getValue('nodeorder_link_to_ordering_page')) - ->set('link_to_ordering_page_taxonomy_admin', $form_state->getValue('nodeorder_link_to_ordering_page_taxonomy_admin')) - ->set('override_taxonomy_page', $form_state->getValue('nodeorder_override_taxonomy_page')) - ->set('vocabularies', $form_state->getValue('nodeorder_vocabularies')) - ->save(); + // @todo Trigger weight update for all nodes in a vocabulary's term tree. + // @see nodeorder_taxonomy_vocabulary_form_submit. + $vocabularies = array_filter($form_state->getValue('nodeorder_vocabularies')); + + $this->configManager->updateConfigValues([ + ConfigManagerInterface::KEY_SHOW_LINKS_ON_NODE => $form_state->getValue('nodeorder_show_links_on_node'), + ConfigManagerInterface::KEY_LINK_TO_ORDERING_PAGE => $form_state->getValue('nodeorder_link_to_ordering_page'), + ConfigManagerInterface::KEY_LINK_TO_ORDERING_PAGE_TAXONOMY_ADMIN => $form_state->getValue('nodeorder_link_to_ordering_page_taxonomy_admin'), + ConfigManagerInterface::KEY_OVERRIDE_TAXONOMY_PAGE => $form_state->getValue('nodeorder_override_taxonomy_page'), + ConfigManagerInterface::KEY_VOCABULARIES => $vocabularies, + ]); parent::submitForm($form, $form_state); } @@ -134,7 +150,7 @@ class NodeorderAdminForm extends ConfigFormBase { * {@inheritdoc} */ protected function getEditableConfigNames() { - return ['nodeorder.settings']; + return [ConfigManagerInterface::CONFIG_NAME]; } } diff --git a/src/NodeOrderManager.php b/src/NodeOrderManager.php index 764842d8e68bed4908e97caead5b0a55e8e14997..7850309b4ff31548c9b19979f318d2dfda0af7e6 100644 --- a/src/NodeOrderManager.php +++ b/src/NodeOrderManager.php @@ -16,11 +16,11 @@ use Drupal\Core\Cache\CacheBackendInterface; class NodeOrderManager implements NodeOrderManagerInterface { /** - * The configuration object. + * The nodeorder config manager. * - * @var \Drupal\Core\Config\ImmutableConfig + * @var \Drupal\nodeorder\ConfigManagerInterface */ - protected $config; + protected $configManager; /** * The current database connection. @@ -57,9 +57,9 @@ class NodeOrderManager implements NodeOrderManagerInterface { * * @throws \Throwable */ - public function __construct(ConfigFactoryInterface $config, Connection $database, EntityTypeManagerInterface $entity_type_manager, CacheBackendInterface $cache) { + public function __construct(ConfigManagerInterface $config_manager, Connection $database, EntityTypeManagerInterface $entity_type_manager, CacheBackendInterface $cache) { $this->database = $database; - $this->config = $config->get('nodeorder.settings'); + $this->configManager = $config_manager; $this->termStorage = $entity_type_manager->getStorage('taxonomy_term'); $this->cache = $cache; } @@ -159,7 +159,7 @@ class NodeOrderManager implements NodeOrderManagerInterface { * {@inheritdoc} */ public function vocabularyIsOrderable($vid) { - $vocabularies = $this->config->get('vocabularies'); + $vocabularies = $this->configManager->config()->get('vocabularies'); return !empty($vocabularies[$vid]); } @@ -167,6 +167,8 @@ class NodeOrderManager implements NodeOrderManagerInterface { * {@inheritdoc} */ public function selectNodes($tids = [], $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC', $count = -1) { + $config = $this->configManager->config(); + if (count($tids) > 0) { // For each term ID, generate an array of descendant term IDs // to the right depth. @@ -207,13 +209,13 @@ class NodeOrderManager implements NodeOrderManagerInterface { if ($pager) { if ($count == -1) { - $count = $this->config->get('default_nodes_main'); + $count = $config->get('default_nodes_main'); } $result = pager_query($sql, $count, 0, $sql_count, $args); } else { if ($count == -1) { - $count = $this->config->get('feed_default_items'); + $count = $config->get('feed_default_items'); } if ($count == 0) { @@ -271,6 +273,7 @@ class NodeOrderManager implements NodeOrderManagerInterface { * {@inheritdoc} */ public function getOrderableTids(NodeInterface $node, $reset = FALSE) { + $config = $this->configManager->config(); $cid = 'nodeorder:orderable_tids:' . $node->getType(); if (!$reset && ($cache = $this->cache->get($cid)) && !empty($cache->data)) { @@ -278,7 +281,7 @@ class NodeOrderManager implements NodeOrderManagerInterface { } else { $vocabularies = []; - foreach ($this->config->get('vocabularies') as $vid => $orderable) { + foreach ($config->get('vocabularies') as $vid => $orderable) { if ($orderable) { $vocabularies[] = $vid; } diff --git a/src/Tests/NodeorderCrudTest.php b/tests/src/Functional/NodeorderCrudTest.php similarity index 83% rename from src/Tests/NodeorderCrudTest.php rename to tests/src/Functional/NodeorderCrudTest.php index b7a2535f171b3e48ac883108d7c15051dda6a873..8f621e699a98d2f34ce28f513f9a995e62bed678 100644 --- a/src/Tests/NodeorderCrudTest.php +++ b/tests/src/Functional/NodeorderCrudTest.php @@ -1,6 +1,6 @@ <?php -namespace Drupal\nodeorder\Tests; +namespace Drupal\Tests\nodeorder\Functional; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Tests\taxonomy\Functional\TaxonomyTestBase; @@ -29,6 +29,13 @@ class NodeorderCrudTest extends TaxonomyTestBase { */ protected $nodeOrderManager; + /** + * The nodeorder config manager. + * + * @var \Drupal\nodeorder\ConfigManagerInterface + */ + protected $configManager; + /** * Taxonomy term reference field for testing. * @@ -46,12 +53,13 @@ class NodeorderCrudTest extends TaxonomyTestBase { /** * {@inheritdoc} * - * @see \Drupal\taxonomy\Tests\TermTest::setUp() + * @see \Drupal\Tests\taxonomy\Functional\TermTest::setUp() */ public function setUp(): void { parent::setUp(); $this->nodeOrderManager = $this->container->get('nodeorder.manager'); + $this->configManager = $this->container->get('nodeorder.config_manager'); $this->drupalLogin($this->drupalCreateUser([ 'administer taxonomy', 'bypass node access', 'order nodes within categories', @@ -105,15 +113,15 @@ class NodeorderCrudTest extends TaxonomyTestBase { */ public function testOrderableVocabulary() { // Vocabulary should default to not being orderable. - $this->assertFalse($this->nodeOrderManager->vocabularyIsOrderable($this->vocabulary->id()), 'The test vocabulary is not orderable by default.'); + $isOrderable = $this->nodeOrderManager->vocabularyIsOrderable($this->vocabulary->id()); + static::assertFalse($isOrderable, 'The test vocabulary is not orderable by default.'); // Enable 'orderable' on this vocabulary. - \Drupal::configFactory()->getEditable('nodeorder.settings') - ->set('vocabularies', [$this->vocabulary->id() => TRUE]) - ->save(); + $this->configManager->updateOrderableValue($this->vocabulary->id(),TRUE); // Ensure the vocabulary is sortable. - $this->assertTrue($this->nodeOrderManager->vocabularyIsOrderable($this->vocabulary->id()), 'The test vocabulary is orderable.'); + $isOrderable = $this->nodeOrderManager->vocabularyIsOrderable($this->vocabulary->id()); + static::assertTrue($isOrderable, 'The test vocabulary is orderable.'); } /** @@ -121,9 +129,7 @@ class NodeorderCrudTest extends TaxonomyTestBase { */ public function testNodeCrudOperations() { // Enable 'orderable' on this vocabulary. - \Drupal::configFactory()->getEditable('nodeorder.settings') - ->set('vocabularies', [$this->vocabulary->id() => TRUE]) - ->save(); + $this->configManager->updateOrderableValue($this->vocabulary->id(),TRUE); // Create two taxonomy terms. $term1 = $this->createTerm($this->vocabulary); @@ -162,7 +168,8 @@ class NodeorderCrudTest extends TaxonomyTestBase { 'entities[' . $node1->id() . '][weight]' => -1, 'entities[' . $node2->id() . '][weight]' => 1, ]; - $this->submitForm('taxonomy/term/' . $term1->id() . '/order', $edit, t('Save')); + $this->drupalGet('taxonomy/term/' . $term1->id() . '/order'); + $this->submitForm($edit, t('Save')); // Order in term 2 should remain unchanged. $this->assertNodeorderByTid($term2->id(), $expected); @@ -201,7 +208,8 @@ class NodeorderCrudTest extends TaxonomyTestBase { 'entities[' . $node2->id() . '][weight]' => 1, 'entities[' . $node3->id() . '][weight]' => 2, ]; - $this->submitForm('taxonomy/term/' . $term1->id() . '/order', $edit, t('Save')); + $this->drupalGet('taxonomy/term/' . $term1->id() . '/order'); + $this->submitForm($edit, t('Save')); // Order in term 2 should remain the same. $this->assertNodeorderByTid($term2->id(), $expected); @@ -258,7 +266,8 @@ class NodeorderCrudTest extends TaxonomyTestBase { 'entities[' . $node3->id() . '][weight]' => 1, 'entities[' . $node4->id() . '][weight]' => 2, ]; - $this->submitForm('taxonomy/term/' . $term1->id() . '/order', $edit, t('Save')); + $this->drupalGet('taxonomy/term/' . $term1->id() . '/order'); + $this->submitForm($edit, t('Save')); // Order in term 2 should remain the same. $this->assertNodeorderByTid($term2->id(), $expected); @@ -297,31 +306,22 @@ class NodeorderCrudTest extends TaxonomyTestBase { * * @param int $tid * Term ID to check order against. - * @param array $expected + * @param array $expectedOrder * Expected order keyed by node ID. */ - protected function assertNodeorderByTid($tid, array $expected) { - $order = \Drupal::service('database')->select('taxonomy_index', 'ti') + protected function assertNodeorderByTid($tid, array $expectedOrder) { + $rows = \Drupal::service('database')->select('taxonomy_index', 'ti') ->fields('ti', ['nid', 'weight']) ->condition('tid', $tid) ->execute() ->fetchAllAssoc('nid'); - foreach ($order as $nid => $row) { - if (isset($expected[$nid]) && $expected[$nid] == $row->weight) { - unset($expected[$nid]); - unset($order[$nid]); - } + $actualOrder = []; + foreach ($rows as $row) { + $actualOrder[$row->nid] = (int) $row->weight; } - if (!empty($expected) || !empty($order)) { - dump($expected, 'Orderings that did not match.'); - dump($order, 'Orderings found that were not expected.'); - $this->fail('Order did not match.'); - } - else { - $this->assertTrue(TRUE, 'Order of nodes matched.'); - } + static::assertEquals($expectedOrder, $actualOrder, 'Order did not match'); } } diff --git a/tests/src/Kernel/ConfigManagerTest.php b/tests/src/Kernel/ConfigManagerTest.php new file mode 100644 index 0000000000000000000000000000000000000000..15c428924c457dc238f9a159cc6be9dd52010032 --- /dev/null +++ b/tests/src/Kernel/ConfigManagerTest.php @@ -0,0 +1,75 @@ +<?php + +namespace Drupal\Tests\nodeorder\Kernel; + +use Drupal\Core\Config\Config; +use Drupal\KernelTests\KernelTestBase; +use Drupal\nodeorder\ConfigManagerInterface; + +/** + * @covers \Drupal\nodeorder\ConfigManager + * @group nodeorder + */ +class ConfigManagerTest extends KernelTestBase { + + /** + * {@inheritdoc} + */ + protected static $modules = ['nodeorder']; + + /** + * The nodeorder configuration manager. + * + * @var \Drupal\nodeorder\ConfigManager + */ + protected $configManager; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + + $this->configManager = $this->container->get('nodeorder.config_manager'); + } + + public function testConfig() { + $this->configManager->updateOrderableValue('test', TRUE); + + $config = $this->configManager->config(); + static::assertInstanceOf(Config::class, $config); + + $actualValues = $config->get(ConfigManagerInterface::KEY_VOCABULARIES); + static::assertEquals(['test' => 'test'], $actualValues); + } + + public function testUpdateOrderableValue() { + $actualValues = function() { + return $this->configManager->config()->get(ConfigManagerInterface::KEY_VOCABULARIES); + }; + + $this->configManager->updateOrderableValue('test2', TRUE); + $this->configManager->updateOrderableValue('test3', TRUE); + static::assertEquals(['test2' => 'test2', 'test3' => 'test3'], $actualValues()); + + $this->configManager->updateOrderableValue('test3', false); + static::assertEquals(['test2' => 'test2'], $actualValues()); + + $this->configManager->updateOrderableValue('test2', false); + static::assertEquals([], $actualValues()); + } + + public function testUpdateConfigValues() { + $this->configManager->updateConfigValues([ + 'testK' => 'testV', + ConfigManagerInterface::KEY_VOCABULARIES => ['test4' => 'test4'], + ]); + + $actualConfigData = $this->configManager->config()->get(); + + static::assertArrayNotHasKey('testK', $actualConfigData); + static::assertArrayHasKey(ConfigManagerInterface::KEY_VOCABULARIES, $actualConfigData); + static::assertEquals(['test4' => 'test4'], $actualConfigData[ConfigManagerInterface::KEY_VOCABULARIES]); + } + +} diff --git a/tests/src/Kernel/NodeorderConfigSchemaTest.php b/tests/src/Kernel/NodeorderConfigSchemaTest.php index 0ffc70bf3480474d2637833267eefc0b147c0419..4de037df378b502d3fd40d1ae39e981a226714a4 100644 --- a/tests/src/Kernel/NodeorderConfigSchemaTest.php +++ b/tests/src/Kernel/NodeorderConfigSchemaTest.php @@ -51,17 +51,12 @@ class NodeorderConfigSchemaTest extends KernelTestBase { public function testConfigSchema($data, $expected) { $config = $this->configuration->setData($data); - $actual = TRUE; - $message = NULL; try { $this->assertConfigSchema($this->typedConfig, $config->getName(), $config->get()); } catch (\Throwable $exception) { - $actual = FALSE; - $message = $exception->getMessage(); + static::fail($exception->getMessage()); } - - $this->assertEquals($expected, $actual, $message); } /**