From f5a9058f64fec8db72176e8244fd8c32f68751b9 Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Fri, 8 Mar 2024 17:09:43 -0800 Subject: [PATCH] Fixup tests --- .../Behavior/ListParagraphBehavior.php | 4 ++-- .../Behavior/TeaserParagraphBehavior.php | 14 +++++++---- .../Behavior/TeaserParagraphBehaviorTest.php | 24 ++++++++++--------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/modules/jumpstart_ui/src/Plugin/paragraphs/Behavior/ListParagraphBehavior.php b/modules/jumpstart_ui/src/Plugin/paragraphs/Behavior/ListParagraphBehavior.php index 259b370d..912ad6cc 100644 --- a/modules/jumpstart_ui/src/Plugin/paragraphs/Behavior/ListParagraphBehavior.php +++ b/modules/jumpstart_ui/src/Plugin/paragraphs/Behavior/ListParagraphBehavior.php @@ -65,8 +65,8 @@ public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, F '#type' => 'radios', '#title' => $this->t('Headline Behavior'), '#options' => [ - self::SHOW_HEADING => $this->t('Display heading: This displays the paragraph headline as an H2. You’ll usually want to choose this option. '), - self::HIDE_HEADING => $this->t('Visually hide heading: For improved accessibility, this keeps the headline in the page structure as an H2, but you won’t see it. '), + self::SHOW_HEADING => $this->t('Display heading: This displays the paragraph headline as an H2. You’ll usually want to choose this option.'), + self::HIDE_HEADING => $this->t('Visually hide heading: For improved accessibility, this keeps the headline in the page structure as an H2, but you won’t see it.'), self::REMOVE_HEADING => $this->t('Remove heading: This completely removes the headline from the page and assumes you have placed an H2 on the page above this paragraph.'), ], '#default_value' => $paragraph->getBehaviorSetting('list_paragraph', 'heading_behavior', self::SHOW_HEADING), diff --git a/modules/jumpstart_ui/src/Plugin/paragraphs/Behavior/TeaserParagraphBehavior.php b/modules/jumpstart_ui/src/Plugin/paragraphs/Behavior/TeaserParagraphBehavior.php index 522c71aa..844e1dbd 100644 --- a/modules/jumpstart_ui/src/Plugin/paragraphs/Behavior/TeaserParagraphBehavior.php +++ b/modules/jumpstart_ui/src/Plugin/paragraphs/Behavior/TeaserParagraphBehavior.php @@ -37,7 +37,7 @@ public static function isApplicable(ParagraphsTypeInterface $paragraphs_type) { * {@inheritDoc} */ public function defaultConfiguration() { - return ['heading_behavior' => 'show',]; + return ['heading_behavior' => 'show']; } /** @@ -49,8 +49,8 @@ public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, F '#type' => 'radios', '#title' => $this->t('Headline Behavior'), '#options' => [ - self::SHOW_HEADING => $this->t('Display heading: This displays the paragraph headline as an H2. You’ll usually want to choose this option. '), - self::HIDE_HEADING => $this->t('Visually hide heading: For improved accessibility, this keeps the headline in the page structure as an H2, but you won’t see it. '), + self::SHOW_HEADING => $this->t('Display heading: This displays the paragraph headline as an H2. You’ll usually want to choose this option.'), + self::HIDE_HEADING => $this->t('Visually hide heading: For improved accessibility, this keeps the headline in the page structure as an H2, but you won’t see it.'), self::REMOVE_HEADING => $this->t('Remove heading: This completely removes the headline from the page and assumes you have placed an H2 on the page above this paragraph.'), ], '#default_value' => $paragraph->getBehaviorSetting('stanford_teaser', 'heading_behavior', self::SHOW_HEADING), @@ -64,8 +64,12 @@ public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, F public function view(array &$build, ParagraphInterface $paragraph, EntityViewDisplayInterface $display, $view_mode) { $heading_behavior = $paragraph->getBehaviorSetting('stanford_teaser', 'heading_behavior', self::SHOW_HEADING); - // Heading is populated or to be removed, change the display mode of the entities. - if (isset($build['su_entity_headline'][0]) || $heading_behavior == self::REMOVE_HEADING) { + // Heading is populated or configured to be removed, change the display mode + // of the entities. + if ( + isset($build['su_entity_item']) && + (isset($build['su_entity_headline'][0]) || $heading_behavior == self::REMOVE_HEADING) + ) { foreach (Element::children($build['su_entity_item']) as $delta) { $build['su_entity_item'][$delta]['#view_mode'] = 'stanford_h3_card'; diff --git a/modules/jumpstart_ui/tests/src/Unit/Plugin/paragraphs/Behavior/TeaserParagraphBehaviorTest.php b/modules/jumpstart_ui/tests/src/Unit/Plugin/paragraphs/Behavior/TeaserParagraphBehaviorTest.php index e555023e..76fffae8 100644 --- a/modules/jumpstart_ui/tests/src/Unit/Plugin/paragraphs/Behavior/TeaserParagraphBehaviorTest.php +++ b/modules/jumpstart_ui/tests/src/Unit/Plugin/paragraphs/Behavior/TeaserParagraphBehaviorTest.php @@ -54,27 +54,29 @@ public function testApplication() { public function testForm() { $plugin = TeaserParagraphBehavior::create(\Drupal::getContainer(), [], '', []); $paragraph = $this->createMock(ParagraphInterface::class); - $paragraph->method('getBehaviorSetting')->willReturn(FALSE); + $paragraph->method('getBehaviorSetting')->willReturn('show'); $form = []; $form_state = new FormState(); $form = $plugin->buildBehaviorForm($paragraph, $form, $form_state); - $this->assertArrayHasKey('hide_heading', $form); - $this->assertFalse($form['hide_heading']['#default_value']); + $this->assertArrayHasKey('heading_behavior', $form); + $this->assertEquals('show', $form['heading_behavior']['#default_value']); } public function testView() { $plugin = TeaserParagraphBehavior::create(\Drupal::getContainer(), [], '', []); - $build = ['su_entity_headline' => ['foo']]; + $paragraph = $this->createMock(Paragraph::class); - $paragraph->method('getBehaviorSetting')->willReturn(TRUE); + $paragraph->method('getBehaviorSetting')->willReturn('hide'); $display = $this->createMock(EntityViewDisplayInterface::class); - $plugin->view($build, $paragraph, $display, 'foo'); - // No changes with no items. - $this->assertEquals(['su_entity_headline' => ['foo']], $build); - $build['su_entity_item'][0] = [ - '#view_mode' => 'foobar', - '#cache' => ['keys' => ['foobar']] + $build = [ + 'su_entity_headline' => ['foo'], + 'su_entity_item' => [ + [ + '#view_mode' => 'foobar', + '#cache' => ['keys' => ['foobar']], + ], + ], ]; $plugin->view($build, $paragraph, $display, 'foo'); $this->assertContains('visually-hidden', $build['su_entity_headline']['#attributes']['class']);