Loading core/modules/views_ui/src/Form/Ajax/ConfigHandlerGroup.php +7 −7 Changes for core/modules/views_ui/src/Form/Ajax/ConfigHandlerGroup.php: 7 added lines, 7 removed lines. Original line number Diff line number Diff line Loading @@ -103,17 +103,17 @@ public function buildForm(array $form, FormStateInterface $form_state) { */ public function submitForm(array &$form, FormStateInterface $form_state) { $view = $form_state->get('view'); $item = &$form_state->get('handler')->options; $type = $form_state->get('type'); $handler = \Drupal::service('views.plugin_managers')->get($type)->getHandler($item); $executable = $view->getExecutable(); $handler->init($executable, $executable->display_handler, $item); // Use the same handler that built the form. When aggregation is enabled, // the display swaps in the aggregation handler, so recreating the handler // from the item would submit with a different handler than the one whose // form elements were displayed. $handler = $form_state->get('handler'); $item = &$handler->options; $handler->submitGroupByForm($form, $form_state); // Store the item back on the view. $executable->setHandler($form_state->get('display_id'), $form_state->get('type'), $form_state->get('id'), $item); $view->getExecutable()->setHandler($form_state->get('display_id'), $form_state->get('type'), $form_state->get('id'), $item); // Write to cache. $view->cacheSet(); Loading core/modules/views_ui/tests/src/Functional/GroupByTest.php +49 −1 Changes for core/modules/views_ui/tests/src/Functional/GroupByTest.php: 49 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -14,12 +14,22 @@ #[RunTestsInSeparateProcesses] class GroupByTest extends UITestBase { /** * {@inheritdoc} */ protected static $modules = [ 'entity_test', ]; /** * Views used by this test. * * @var array */ public static $testViews = ['test_views_group_by_save']; public static $testViews = [ 'test_aggregate_count', 'test_views_group_by_save', ]; /** * {@inheritdoc} Loading Loading @@ -59,4 +69,42 @@ public function testGroupBySave(): void { $this->assertEquals('count', $display['display_options']['fields']['id']['group_type'], 'Count group_by_type was saved on the view.'); } /** * Tests that the handler that builds the form also submits it. */ public function testAggregationHandlerIsReusedOnSubmit(): void { $edit_handler_url = 'admin/structure/views/nojs/handler-group/test_aggregate_count/default/field/id'; $this->drupalGet($edit_handler_url); // Without aggregation applied yet the entity field handler builds the // form, which includes the group column selects. $this->assertSession()->fieldValueEquals('options[group_type]', 'group'); $this->assertSession()->fieldExists('options[group_column]'); $this->assertSession()->fieldExists('options[group_columns][bundle]'); $this->submitForm(['options[group_type]' => 'count'], 'Apply'); $this->assertSession()->statusCodeEquals(200); // With aggregation applied the numeric handler builds the form, which // has no group column selects. $this->drupalGet($edit_handler_url); $this->assertSession()->fieldValueEquals('options[group_type]', 'count'); $this->assertSession()->fieldNotExists('options[group_column]'); $this->assertSession()->fieldNotExists('options[group_columns][bundle]'); $this->submitForm(['options[group_type]' => 'count'], 'Apply'); // Ensure that there is no error after submitting the form. $this->assertSession()->statusCodeEquals(200); // Switching back to "Group results together" restores the entity field // handler and its group column selects. $this->drupalGet($edit_handler_url); $this->assertSession()->fieldValueEquals('options[group_type]', 'count'); $this->assertSession()->fieldNotExists('options[group_column]'); $this->assertSession()->fieldNotExists('options[group_columns][bundle]'); $this->submitForm(['options[group_type]' => 'group'], 'Apply'); $this->assertSession()->statusCodeEquals(200); $this->drupalGet($edit_handler_url); $this->assertSession()->fieldValueEquals('options[group_type]', 'group'); $this->assertSession()->fieldExists('options[group_column]'); $this->assertSession()->fieldExists('options[group_columns][bundle]'); } } core/modules/views_ui/tests/src/FunctionalJavascript/AggregationAjaxTest.php +169 Changes for core/modules/views_ui/tests/src/FunctionalJavascript/AggregationAjaxTest.php: 169 added lines, 0 removed lines. Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\views_ui\FunctionalJavascript; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; use Drupal\views\Tests\ViewTestData; use Drupal\views\Views; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; /** * Tests editing a field's aggregation function via AJAX. * * @see https://www.drupal.org/project/drupal/issues/3344910 */ #[Group('views_ui')] #[RunTestsInSeparateProcesses] class AggregationAjaxTest extends WebDriverTestBase { /** * {@inheritdoc} */ protected static $modules = ['node', 'views', 'views_ui', 'views_test_config']; /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; /** * The views used for testing. * * @var array */ public static $testViews = ['test_content_ajax']; /** * The message Drupal shows on the page for an unhandled AJAX error. */ protected const AJAX_ERROR_MESSAGE = "Oops, something went wrong. Check your browser's developer console for more details."; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); // Disable automatic live preview to make the sequence of calls clearer. \Drupal::configFactory()->getEditable('views.settings')->set('ui.always_live_preview', FALSE)->save(); ViewTestData::createTestViews(self::class, ['views_test_config']); $admin_user = $this->drupalCreateUser(['administer views']); $this->drupalLogin($admin_user); } /** * Tests switching a field's aggregation function via the AJAX dialog. */ public function testSwitchAggregationFunctionViaAjax(): void { $page = $this->getSession()->getPage(); $assert_session = $this->assertSession(); // Add an isolated runtime display with a single field. $view_name = static::$testViews[0]; $view = Views::getView($view_name); $display_id = $view->storage->addDisplay('page'); $display = &$view->storage->getDisplay($display_id); $display['display_options']['defaults']['fields'] = FALSE; $display['display_options']['fields'] = [ 'changed' => [ 'id' => 'changed', 'table' => 'node_field_data', 'field' => 'changed', 'relationship' => 'none', 'group_type' => 'group', 'plugin_id' => 'field', 'entity_type' => 'node', 'entity_field' => 'changed', ], ]; $view->save(); $this->drupalGet('admin/structure/views/view/' . $view_name . '/edit/' . $display_id); // Enable aggregation for the display. $this->openDisplayOptionDialog('group_by'); $page->checkField('group_by'); $this->pressDialogButton('Apply'); $this->assertNotEmpty( $assert_session->waitForElementRemoved('css', '.views-ui-dialog'), 'The "Use aggregation" dialog is still open after applying the change.' ); // Configure the field's aggregation function for the first time. $this->setFieldAggregationFunction('count'); // Re-open the same, already-configured handler and switch the // aggregation function from COUNT to COUNT DISTINCT. $this->setFieldAggregationFunction('count_distinct'); // Verify that the link to the aggregation settings is still present. $assert_session->linkExists('Aggregation settings'); } /** * Opens the AJAX dialog for a display-level "Advanced" setting. * * @param string $option_id * The machine name of the display option to open, e.g. 'group_by'. */ protected function openDisplayOptionDialog(string $option_id): void { $assert_session = $this->assertSession(); $link = $assert_session->waitForElementVisible('css', sprintf('a[href$="/%s"]', $option_id)); $this->assertNotEmpty($link, sprintf('The link for the "%s" display option was not found.', $option_id)); $link->click(); $this->assertNotEmpty( $assert_session->waitForElementVisible('css', '.views-ui-dialog'), sprintf('The dialog for the "%s" display option did not open.', $option_id) ); } /** * Opens the aggregation settings dialog and applies an aggregation function to the 'changed' field. * * @param string $group_type * The aggregation function to select, e.g. 'count' or 'count_distinct'. */ protected function setFieldAggregationFunction(string $group_type): void { $page = $this->getSession()->getPage(); $assert_session = $this->assertSession(); // Opens Aggregation settings dialog. $page->clickLink('Aggregation settings'); $this->assertNotEmpty( $assert_session->waitForElementVisible('css', '.views-ui-dialog'), sprintf('The aggregation settings dialog did not open while setting "%s".', $group_type) ); $assert_session->fieldExists('options[group_type]'); $page->selectFieldOption('options[group_type]', $group_type); $this->pressDialogButton('Apply'); // Check if the settings dialog closes. $this->assertNotEmpty( $assert_session->waitForElementRemoved('css', '.views-ui-dialog'), sprintf('The aggregation settings dialog is still open after setting "%s".', $group_type) ); // Check for the AJAX error message. $assert_session->pageTextNotContains(static::AJAX_ERROR_MESSAGE); } /** * Presses a button inside the currently open Views UI modal dialog. * * @param string $text * The visible text of the button to press. */ protected function pressDialogButton(string $text): void { $this->assertSession() ->elementExists('css', sprintf('.ui-dialog-buttonset button:contains("%s")', $text)) ->press(); } } Loading
core/modules/views_ui/src/Form/Ajax/ConfigHandlerGroup.php +7 −7 Changes for core/modules/views_ui/src/Form/Ajax/ConfigHandlerGroup.php: 7 added lines, 7 removed lines. Original line number Diff line number Diff line Loading @@ -103,17 +103,17 @@ public function buildForm(array $form, FormStateInterface $form_state) { */ public function submitForm(array &$form, FormStateInterface $form_state) { $view = $form_state->get('view'); $item = &$form_state->get('handler')->options; $type = $form_state->get('type'); $handler = \Drupal::service('views.plugin_managers')->get($type)->getHandler($item); $executable = $view->getExecutable(); $handler->init($executable, $executable->display_handler, $item); // Use the same handler that built the form. When aggregation is enabled, // the display swaps in the aggregation handler, so recreating the handler // from the item would submit with a different handler than the one whose // form elements were displayed. $handler = $form_state->get('handler'); $item = &$handler->options; $handler->submitGroupByForm($form, $form_state); // Store the item back on the view. $executable->setHandler($form_state->get('display_id'), $form_state->get('type'), $form_state->get('id'), $item); $view->getExecutable()->setHandler($form_state->get('display_id'), $form_state->get('type'), $form_state->get('id'), $item); // Write to cache. $view->cacheSet(); Loading
core/modules/views_ui/tests/src/Functional/GroupByTest.php +49 −1 Changes for core/modules/views_ui/tests/src/Functional/GroupByTest.php: 49 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -14,12 +14,22 @@ #[RunTestsInSeparateProcesses] class GroupByTest extends UITestBase { /** * {@inheritdoc} */ protected static $modules = [ 'entity_test', ]; /** * Views used by this test. * * @var array */ public static $testViews = ['test_views_group_by_save']; public static $testViews = [ 'test_aggregate_count', 'test_views_group_by_save', ]; /** * {@inheritdoc} Loading Loading @@ -59,4 +69,42 @@ public function testGroupBySave(): void { $this->assertEquals('count', $display['display_options']['fields']['id']['group_type'], 'Count group_by_type was saved on the view.'); } /** * Tests that the handler that builds the form also submits it. */ public function testAggregationHandlerIsReusedOnSubmit(): void { $edit_handler_url = 'admin/structure/views/nojs/handler-group/test_aggregate_count/default/field/id'; $this->drupalGet($edit_handler_url); // Without aggregation applied yet the entity field handler builds the // form, which includes the group column selects. $this->assertSession()->fieldValueEquals('options[group_type]', 'group'); $this->assertSession()->fieldExists('options[group_column]'); $this->assertSession()->fieldExists('options[group_columns][bundle]'); $this->submitForm(['options[group_type]' => 'count'], 'Apply'); $this->assertSession()->statusCodeEquals(200); // With aggregation applied the numeric handler builds the form, which // has no group column selects. $this->drupalGet($edit_handler_url); $this->assertSession()->fieldValueEquals('options[group_type]', 'count'); $this->assertSession()->fieldNotExists('options[group_column]'); $this->assertSession()->fieldNotExists('options[group_columns][bundle]'); $this->submitForm(['options[group_type]' => 'count'], 'Apply'); // Ensure that there is no error after submitting the form. $this->assertSession()->statusCodeEquals(200); // Switching back to "Group results together" restores the entity field // handler and its group column selects. $this->drupalGet($edit_handler_url); $this->assertSession()->fieldValueEquals('options[group_type]', 'count'); $this->assertSession()->fieldNotExists('options[group_column]'); $this->assertSession()->fieldNotExists('options[group_columns][bundle]'); $this->submitForm(['options[group_type]' => 'group'], 'Apply'); $this->assertSession()->statusCodeEquals(200); $this->drupalGet($edit_handler_url); $this->assertSession()->fieldValueEquals('options[group_type]', 'group'); $this->assertSession()->fieldExists('options[group_column]'); $this->assertSession()->fieldExists('options[group_columns][bundle]'); } }
core/modules/views_ui/tests/src/FunctionalJavascript/AggregationAjaxTest.php +169 Changes for core/modules/views_ui/tests/src/FunctionalJavascript/AggregationAjaxTest.php: 169 added lines, 0 removed lines. Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\views_ui\FunctionalJavascript; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; use Drupal\views\Tests\ViewTestData; use Drupal\views\Views; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; /** * Tests editing a field's aggregation function via AJAX. * * @see https://www.drupal.org/project/drupal/issues/3344910 */ #[Group('views_ui')] #[RunTestsInSeparateProcesses] class AggregationAjaxTest extends WebDriverTestBase { /** * {@inheritdoc} */ protected static $modules = ['node', 'views', 'views_ui', 'views_test_config']; /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; /** * The views used for testing. * * @var array */ public static $testViews = ['test_content_ajax']; /** * The message Drupal shows on the page for an unhandled AJAX error. */ protected const AJAX_ERROR_MESSAGE = "Oops, something went wrong. Check your browser's developer console for more details."; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); // Disable automatic live preview to make the sequence of calls clearer. \Drupal::configFactory()->getEditable('views.settings')->set('ui.always_live_preview', FALSE)->save(); ViewTestData::createTestViews(self::class, ['views_test_config']); $admin_user = $this->drupalCreateUser(['administer views']); $this->drupalLogin($admin_user); } /** * Tests switching a field's aggregation function via the AJAX dialog. */ public function testSwitchAggregationFunctionViaAjax(): void { $page = $this->getSession()->getPage(); $assert_session = $this->assertSession(); // Add an isolated runtime display with a single field. $view_name = static::$testViews[0]; $view = Views::getView($view_name); $display_id = $view->storage->addDisplay('page'); $display = &$view->storage->getDisplay($display_id); $display['display_options']['defaults']['fields'] = FALSE; $display['display_options']['fields'] = [ 'changed' => [ 'id' => 'changed', 'table' => 'node_field_data', 'field' => 'changed', 'relationship' => 'none', 'group_type' => 'group', 'plugin_id' => 'field', 'entity_type' => 'node', 'entity_field' => 'changed', ], ]; $view->save(); $this->drupalGet('admin/structure/views/view/' . $view_name . '/edit/' . $display_id); // Enable aggregation for the display. $this->openDisplayOptionDialog('group_by'); $page->checkField('group_by'); $this->pressDialogButton('Apply'); $this->assertNotEmpty( $assert_session->waitForElementRemoved('css', '.views-ui-dialog'), 'The "Use aggregation" dialog is still open after applying the change.' ); // Configure the field's aggregation function for the first time. $this->setFieldAggregationFunction('count'); // Re-open the same, already-configured handler and switch the // aggregation function from COUNT to COUNT DISTINCT. $this->setFieldAggregationFunction('count_distinct'); // Verify that the link to the aggregation settings is still present. $assert_session->linkExists('Aggregation settings'); } /** * Opens the AJAX dialog for a display-level "Advanced" setting. * * @param string $option_id * The machine name of the display option to open, e.g. 'group_by'. */ protected function openDisplayOptionDialog(string $option_id): void { $assert_session = $this->assertSession(); $link = $assert_session->waitForElementVisible('css', sprintf('a[href$="/%s"]', $option_id)); $this->assertNotEmpty($link, sprintf('The link for the "%s" display option was not found.', $option_id)); $link->click(); $this->assertNotEmpty( $assert_session->waitForElementVisible('css', '.views-ui-dialog'), sprintf('The dialog for the "%s" display option did not open.', $option_id) ); } /** * Opens the aggregation settings dialog and applies an aggregation function to the 'changed' field. * * @param string $group_type * The aggregation function to select, e.g. 'count' or 'count_distinct'. */ protected function setFieldAggregationFunction(string $group_type): void { $page = $this->getSession()->getPage(); $assert_session = $this->assertSession(); // Opens Aggregation settings dialog. $page->clickLink('Aggregation settings'); $this->assertNotEmpty( $assert_session->waitForElementVisible('css', '.views-ui-dialog'), sprintf('The aggregation settings dialog did not open while setting "%s".', $group_type) ); $assert_session->fieldExists('options[group_type]'); $page->selectFieldOption('options[group_type]', $group_type); $this->pressDialogButton('Apply'); // Check if the settings dialog closes. $this->assertNotEmpty( $assert_session->waitForElementRemoved('css', '.views-ui-dialog'), sprintf('The aggregation settings dialog is still open after setting "%s".', $group_type) ); // Check for the AJAX error message. $assert_session->pageTextNotContains(static::AJAX_ERROR_MESSAGE); } /** * Presses a button inside the currently open Views UI modal dialog. * * @param string $text * The visible text of the button to press. */ protected function pressDialogButton(string $text): void { $this->assertSession() ->elementExists('css', sprintf('.ui-dialog-buttonset button:contains("%s")', $text)) ->press(); } }