Verified Commit bea4f5e4 authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

fix: #3618586 default_admin loses the footer group on forms that set #tree,...

fix: #3618586 default_admin loses the footer group on forms that set #tree, silently unpublishing entities on save

By: f0ns
By: dcam
By: smustgrave
(cherry picked from commit eaba66f4)
parent 802f07cf
Loading
Loading
Loading
Loading
Loading
+5 −1
Changes for core/themes/default_admin/src/Hook/FormHooks.php: 5 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -737,8 +737,12 @@ public function stickyActionButtonsAndSidebar(array &$form, FormStateInterface $
        ],
      ],
    ];
    // Copy footer over.
    // Copy footer over. A group is identified by its #parents, so pin them to
    // those of the original: on forms that set #tree the copy would otherwise
    // register under a different name and elements with '#group' => 'footer'
    // would resolve to the original, which this theme does not render.
    $form['default_admin_sidebar']['footer'] = ($form['footer']) ?? [];
    $form['default_admin_sidebar']['footer']['#parents'] = ['footer'];

    // Sidebar close button.
    $close_sidebar_translation = $this->t('Close sidebar panel');
+5 −0
Changes for core/themes/default_admin/tests/modules/default_admin_form_test/default_admin_form_test.info.yml: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
name: 'Default Admin form test'
type: module
description: 'Provides form alters for testing the Default Admin theme.'
package: Testing
version: VERSION
+26 −0
Changes for core/themes/default_admin/tests/modules/default_admin_form_test/src/Hook/DefaultAdminFormTestHooks.php: 26 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\default_admin_form_test\Hook;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Hook\Attribute\Hook;

/**
 * Hook implementations for default_admin_form_test.
 */
class DefaultAdminFormTestHooks {

  /**
   * Implements hook_form_BASE_FORM_ID_alter() for node_form.
   *
   * Entity forms are allowed to set #tree, and some contributed ones do, so
   * the node form stands in for them here.
   */
  #[Hook('form_node_form_alter')]
  public function formNodeFormAlter(array &$form, FormStateInterface $form_state): void {
    $form['#tree'] = TRUE;
  }

}
+76 −0
Changes for core/themes/default_admin/tests/src/Functional/AdminNodeTest.php: 76 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\default_admin\Functional;

use Drupal\Tests\BrowserTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests Default Admin theme for nodes.
 */
#[Group('default_admin')]
#[RunTestsInSeparateProcesses]
class AdminNodeTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'node',
    'default_admin_form_test',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'default_admin';

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->container->get('config.factory')
      ->getEditable('system.theme')
      ->set('admin', 'default_admin')
      ->save();

    $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
    $this->drupalLogin($this->drupalCreateUser([
      'access administration pages',
      'view the administration theme',
      'create page content',
      'edit any page content',
      'administer nodes',
    ]));
  }

  /**
   * Tests the published checkbox on an entity form that sets #tree.
   *
   * The theme copies the footer into its sidebar, and a group is identified by
   * its #parents, so without pinning them the copy registers under a different
   * name on a treed form. The checkbox would then render nowhere while still
   * being processed on submit, which unpublishes the entity on save.
   */
  public function testPublishedCheckboxOnTreedForm(): void {
    $node = $this->drupalCreateNode(['type' => 'page', 'status' => TRUE]);

    $this->drupalGet($node->toUrl('edit-form'));
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->fieldExists('status[value]');
    $this->assertSession()->checkboxChecked('status[value]');

    $this->submitForm([], 'Save');

    $node = $this->container->get('entity_type.manager')
      ->getStorage('node')
      ->loadUnchanged($node->id());
    $this->assertTrue($node->isPublished());
  }

}
+1 −2
Changes for core/themes/default_admin/tests/src/Functional/AdminTest.php: 1 added line, 2 removed lines.
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ class AdminTest extends BrowserTestBase {
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';
  protected $defaultTheme = 'default_admin';

  /**
   * {@inheritdoc}
@@ -35,7 +35,6 @@ class AdminTest extends BrowserTestBase {
  protected function setUp(): void {
    parent::setUp();

    $this->assertTrue(\Drupal::service('theme_installer')->install(['default_admin']));
    $this->container->get('config.factory')
      ->getEditable('system.theme')
      ->set('default', 'default_admin')