From 9cf9edde355a19568446d941dd7115ea62003404 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Thu, 9 May 2013 21:27:50 +0100 Subject: [PATCH] Issue #1912460 by damiankloip, dawehner: Move views_array_key_plus() to RearrangeFilter class. --- .../Drupal/Tests/views/UI/ViewsUITest.php | 45 +++++++++++++++++++ core/modules/views/views.module | 16 ------- .../views_ui/Form/Ajax/RearrangeFilter.php | 29 +++++++++++- 3 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 core/modules/views/tests/Drupal/Tests/views/UI/ViewsUITest.php diff --git a/core/modules/views/tests/Drupal/Tests/views/UI/ViewsUITest.php b/core/modules/views/tests/Drupal/Tests/views/UI/ViewsUITest.php new file mode 100644 index 000000000000..5ae1122d3c6d --- /dev/null +++ b/core/modules/views/tests/Drupal/Tests/views/UI/ViewsUITest.php @@ -0,0 +1,45 @@ +<?php + +/** + * @file + * Contains \Drupal\Tests\UI\ViewsUITest. + */ + +namespace Drupal\Tests\views\UI; + +use Drupal\Tests\UnitTestCase; +use Drupal\views_ui\Form\Ajax\RearrangeFilter; + +/** + * Tests views_ui functions and methods. + * + * @group Views UI + */ +class ViewsUITest extends UnitTestCase { + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('views', 'views_ui'); + + public static function getInfo() { + return array( + 'name' => 'Module tests', + 'description' => 'Unit tests for Views UI module functions.', + 'group' => 'Views UI', + ); + } + + /** + * Tests static methods. + */ + public function testStaticMethods() { + // Test the RearrangeFilter::arrayKeyPlus method. + $original = array(0 => 'one', 1 => 'two', 2 => 'three'); + $expected = array(1 => 'one', 2 => 'two', 3 => 'three'); + $this->assertSame(RearrangeFilter::arrayKeyPlus($original), $expected); + } + +} diff --git a/core/modules/views/views.module b/core/modules/views/views.module index f2b83ca18de9..581c04b8bee0 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -1770,22 +1770,6 @@ function _field_view_formatter_options($field_type = NULL) { return $options; } -/** - * Adds one to each key of the array. - * - * For example array(0 => 'foo') would be array(1 => 'foo'). - */ -function views_array_key_plus($array) { - $keys = array_keys($array); - rsort($keys); - foreach ($keys as $key) { - $array[$key+1] = $array[$key]; - unset($array[$key]); - } - asort($array); - return $array; -} - /** * Set a cached item in the views cache. * diff --git a/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/RearrangeFilter.php b/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/RearrangeFilter.php index ce461b9c1259..ff5db0ab20f0 100644 --- a/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/RearrangeFilter.php +++ b/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/RearrangeFilter.php @@ -293,11 +293,11 @@ public function submitForm(array &$form, array &$form_state) { // The actual update button was clicked. Remove the empty groups, and // renumber them sequentially. ksort($remember_groups); - $groups['groups'] = views_array_key_plus(array_values(array_intersect_key($groups['groups'], $remember_groups))); + $groups['groups'] = static::arrayKeyPlus(array_values(array_intersect_key($groups['groups'], $remember_groups))); // Change the 'group' key on each field to match. Here, $mapping is an // array whose keys are the old group numbers and whose values are the new // (sequentially numbered) ones. - $mapping = array_flip(views_array_key_plus(array_keys($remember_groups))); + $mapping = array_flip(static::arrayKeyPlus(array_keys($remember_groups))); foreach ($new_fields as &$new_field) { $new_field['group'] = $mapping[$new_field['group']]; } @@ -314,5 +314,30 @@ public function submitForm(array &$form, array &$form_state) { $form_state['view']->cacheSet(); } + /** + * Adds one to each key of an array. + * + * For example array(0 => 'foo') would be array(1 => 'foo'). + * + * @param array + * The array to increment keys on. + * + * @return array + * The array with incremented keys. + */ + public static function arrayKeyPlus($array) { + $keys = array_keys($array); + // Sort the keys in reverse order so incrementing them doesn't overwrite any + // existing keys. + rsort($keys); + foreach ($keys as $key) { + $array[$key + 1] = $array[$key]; + unset($array[$key]); + } + // Sort the keys back to ascending order. + ksort($array); + return $array; + } + } -- GitLab