diff --git a/core_search_facets/src/Tests/IntegrationTest.php b/core_search_facets/src/Tests/IntegrationTest.php index f600e38..8affe4b 100644 --- a/core_search_facets/src/Tests/IntegrationTest.php +++ b/core_search_facets/src/Tests/IntegrationTest.php @@ -67,12 +67,12 @@ class IntegrationTest extends WebTestBase { // Verify that the number of results per item. $this->drupalGet('search/node', ['query' => ['keys' => 'test']]); - $this->assertLink('page (19)'); - $this->assertLink('article (10)'); + $this->assertRaw('page (19)'); + $this->assertRaw('article (10)'); // Verify that the label is correct for a clicked link. - $this->clickLink('page (19)'); - $this->assertLink('(-) page (19)'); + $this->clickLinkPartialName('page'); + $this->assertRaw('(-) page (19)'); // Do not show the block on empty behaviors. // Truncate the search_index table because, for the moment, we don't have @@ -113,27 +113,27 @@ class IntegrationTest extends WebTestBase { // Assert date facets. $this->drupalGet('search/node', ['query' => ['keys' => 'test']]); - $this->assertLink('February 2016 (9)'); - $this->assertLink('March 2016 (10)'); - $this->assertLink('April 2016 (10)'); + $this->assertRaw('February 2016 (9)'); + $this->assertRaw('March 2016 (10)'); + $this->assertRaw('April 2016 (10)'); $this->assertResponse(200); - $this->clickLink('March 2016 (10)'); + $this->clickLinkPartialName('March 2016'); $this->assertResponse(200); - $this->assertLink('March 8, 2016 (1)'); - $this->assertLink('March 9, 2016 (2)'); + $this->assertRaw('March 8, 2016 (1)'); + $this->assertRaw('March 9, 2016 (2)'); - $this->clickLink('March 9, 2016 (2)'); + $this->clickLinkPartialName('March 9'); $this->assertResponse(200); - $this->assertLink('10 AM (1)'); - $this->assertLink('12 PM (1)'); + $this->assertRaw('10 AM (1)'); + $this->assertRaw('12 PM (1)'); $this->drupalGet('search/node', ['query' => ['keys' => 'test']]); - $this->assertLink('April 2016 (10)'); - $this->clickLink('April 2016 (10)'); + $this->assertRaw('April 2016 (10)'); + $this->clickLinkPartialName('April 2016'); $this->assertResponse(200); - $this->assertLink('April 1, 2016 (1)'); - $this->assertLink('April 2, 2016 (1)'); + $this->assertRaw('April 1, 2016 (1)'); + $this->assertRaw('April 2, 2016 (1)'); } /** @@ -159,11 +159,11 @@ class IntegrationTest extends WebTestBase { search_update_totals(); $this->drupalGet('search/node', ['query' => ['keys' => 'test']]); - $this->assertLink('December 2016 (1)'); - $this->clickLink('December 2016 (1)'); + $this->assertRaw('December 2016 (1)'); + $this->clickLinkPartialName('December 2016'); $this->assertResponse(200); - $this->assertLink('December 3, 2016 (1)'); - $this->clickLink('December 3, 2016 (1)'); + $this->assertRaw('December 3, 2016 (1)'); + $this->clickLinkPartialName('December 3, 2016'); $this->assertResponse(200); } diff --git a/src/Plugin/facets/widget/LinksWidget.php b/src/Plugin/facets/widget/LinksWidget.php index 774ba04..31db9ee 100644 --- a/src/Plugin/facets/widget/LinksWidget.php +++ b/src/Plugin/facets/widget/LinksWidget.php @@ -2,6 +2,7 @@ namespace Drupal\facets\Plugin\facets\widget; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Link; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -189,12 +190,12 @@ class LinksWidget implements WidgetInterface { * The text to display. */ protected function extractText(ResultInterface $result) { - $text = $result->getDisplayValue(); + $text = new FormattableMarkup('@text', ['@text' => $result->getDisplayValue(), '@count' => $result->getCount()]); if ($this->showNumbers && $result->getCount()) { - $text .= ' (' . $result->getCount() . ')'; + $text->string .= ' (@count)'; } if ($result->isActive()) { - $text = '(-) ' . $text; + $text->string = '(-) ' . $text->string; } return $text; } diff --git a/src/Tests/ExampleContentTrait.php b/src/Tests/ExampleContentTrait.php index fe28f9f..140ce1a 100644 --- a/src/Tests/ExampleContentTrait.php +++ b/src/Tests/ExampleContentTrait.php @@ -129,8 +129,8 @@ trait ExampleContentTrait { * Another string. */ protected function assertStringPosition($x, $y) { - $this->assertText($x); - $this->assertText($y); + $this->assertRaw($x); + $this->assertRaw($y); $x_position = strpos($this->getRawContent(), $x); $y_position = strpos($this->getRawContent(), $y); diff --git a/src/Tests/IntegrationTest.php b/src/Tests/IntegrationTest.php index c3259d5..f667840 100644 --- a/src/Tests/IntegrationTest.php +++ b/src/Tests/IntegrationTest.php @@ -353,7 +353,7 @@ class IntegrationTest extends WebTestBase { $this->assertLink('article'); $this->clickLink('item'); - $this->assertLink('(-) item'); + $this->assertRaw('(-) item'); $this->assertNoLink('article'); $this->drupalGet($facet_edit_page); @@ -363,7 +363,7 @@ class IntegrationTest extends WebTestBase { $this->assertLink('article'); $this->clickLink('item'); - $this->assertLink('(-) item'); + $this->assertRaw('(-) item'); $this->assertLink('article'); } @@ -429,7 +429,7 @@ class IntegrationTest extends WebTestBase { $this->assertLink('item'); $this->clickLink('item'); - $this->assertLink('(-) item'); + $this->assertRaw('(-) item'); $this->assertText('foo baz'); $this->assertText('bar baz'); $this->assertNoText('foo bar baz'); @@ -445,7 +445,7 @@ class IntegrationTest extends WebTestBase { $this->assertLink('item'); $this->clickLink('item'); - $this->assertLink('(-) item'); + $this->assertRaw('(-) item'); $this->assertText('foo bar baz'); $this->assertText('foo test'); $this->assertText('bar'); @@ -474,13 +474,13 @@ class IntegrationTest extends WebTestBase { $this->clickLink('grape'); $this->assertText('Displaying 3 search results'); - $this->assertLink('(-) grape'); + $this->assertRaw('(-) grape'); $this->assertLink('orange'); $this->clickLink('orange'); $this->assertText('Displaying 3 search results'); $this->assertLink('grape'); - $this->assertLink('(-) orange'); + $this->assertRaw('(-) orange'); } /** diff --git a/src/Tests/ProcessorIntegrationTest.php b/src/Tests/ProcessorIntegrationTest.php index ddd1439..a4a7fee 100644 --- a/src/Tests/ProcessorIntegrationTest.php +++ b/src/Tests/ProcessorIntegrationTest.php @@ -158,8 +158,9 @@ class ProcessorIntegrationTest extends WebTestBase { $this->drupalGet('search-api-test-fulltext'); $this->assertText('Displaying 10 search results'); - $this->assertText('grape (6)'); + $this->assertRaw('grape (6)'); $this->assertNoText('apple (4)'); + $this->assertNoRaw('apple (4)'); $form = [ 'widget_configs[show_numbers]' => TRUE, @@ -286,7 +287,7 @@ class ProcessorIntegrationTest extends WebTestBase { $this->drupalGet('search-api-test-fulltext'); $this->clickLink('strawberry'); - $this->assertStringPosition('(-) strawberry', 'grape'); + $this->assertStringPosition('(-) strawberry', 'grape'); $form = [ 'facet_sorting[active_widget_order][status]' => TRUE, @@ -296,7 +297,7 @@ class ProcessorIntegrationTest extends WebTestBase { $this->drupalGet('search-api-test-fulltext'); $this->clickLink('strawberry'); - $this->assertStringPosition('grape', '(-) strawberry'); + $this->assertStringPosition('grape', '(-) strawberry'); $form = [ 'facet_sorting[active_widget_order][status]' => FALSE, diff --git a/src/Tests/UrlIntegrationTest.php b/src/Tests/UrlIntegrationTest.php index cfe1a26..4d3107f 100644 --- a/src/Tests/UrlIntegrationTest.php +++ b/src/Tests/UrlIntegrationTest.php @@ -184,7 +184,7 @@ class UrlIntegrationTest extends WebTestBase { // Make sure 'test:colon' is active. $url = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['f[0]' => 'water_bear:test:colon']]); $this->assertUrl($url); - $this->assertLink('(-) test:colon'); + $this->assertRaw('(-) test:colon'); $this->assertLink('orange'); $this->assertLink('banana'); } @@ -204,7 +204,7 @@ class UrlIntegrationTest extends WebTestBase { $this->clickLink('item'); $this->assertResponse(200); - $this->assertLink('(-) item'); + $this->assertRaw('(-) item'); $this->assertLink('article'); $this->assertUrl($url); } diff --git a/src/Tests/WidgetIntegrationTest.php b/src/Tests/WidgetIntegrationTest.php index 658b303..871bd3d 100644 --- a/src/Tests/WidgetIntegrationTest.php +++ b/src/Tests/WidgetIntegrationTest.php @@ -180,7 +180,7 @@ class WidgetIntegrationTest extends WebTestBase { $this->assertLink('article'); $this->clickLink('item'); - $this->assertLink('(-) item'); + $this->assertRaw('(-) item'); } /** @@ -267,8 +267,8 @@ class WidgetIntegrationTest extends WebTestBase { // Go back to the same view and check that links now display the count. $this->drupalGet('search-api-test-fulltext'); - $this->assertLink('item (3)'); - $this->assertLink('article (2)'); + $this->assertRaw('item (3)'); + $this->assertRaw('article (2)'); $this->drupalGet($facet_edit_page); $this->drupalPostForm(NULL, ['widget' => 'links', 'widget_configs[show_numbers]' => FALSE], $this->t('Save')); diff --git a/tests/src/Unit/Plugin/widget/LinksWidgetTest.php b/tests/src/Unit/Plugin/widget/LinksWidgetTest.php index ec3fa76..ae2b6f4 100644 --- a/tests/src/Unit/Plugin/widget/LinksWidgetTest.php +++ b/tests/src/Unit/Plugin/widget/LinksWidgetTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\facets\Unit\Plugin\widget; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Url; use Drupal\facets\Entity\Facet; use Drupal\facets\Plugin\facets\widget\LinksWidget; @@ -64,10 +65,16 @@ class LinksWidgetTest extends UnitTestCase { $this->assertInternalType('array', $output); $this->assertCount(4, $output['#items']); - $expected_links = ['Llama (10)', 'Badger (20)', 'Duck (15)', 'Alpaca (9)']; + $expected_links = [ + $this->buildLinkAssertion('Llama', 10), + $this->buildLinkAssertion('Badger', 20), + $this->buildLinkAssertion('Duck', 15), + $this->buildLinkAssertion('Alpaca', 9), + ]; foreach ($expected_links as $index => $value) { $this->assertInternalType('array', $output['#items'][$index]); $this->assertEquals($value, $output['#items'][$index]['#title']); + $this->assertInstanceOf(FormattableMarkup::class, $output['#items'][$index]['#title']); $this->assertEquals('link', $output['#items'][$index]['#type']); $this->assertEquals(['facet-item'], $output['#items'][$index]['#wrapper_attributes']['class']); } @@ -91,10 +98,10 @@ class LinksWidgetTest extends UnitTestCase { $this->assertCount(4, $output['#items']); $expected_links = [ - '(-) Llama (10)', - 'Badger (20)', - 'Duck (15)', - '(-) Alpaca (9)', + $this->buildLinkAssertion('Llama', 10, TRUE), + $this->buildLinkAssertion('Badger', 20), + $this->buildLinkAssertion('Duck', 15), + $this->buildLinkAssertion('Alpaca', 9, TRUE), ]; foreach ($expected_links as $index => $value) { $this->assertInternalType('array', $output['#items'][$index]); @@ -123,7 +130,12 @@ class LinksWidgetTest extends UnitTestCase { $this->assertInternalType('array', $output); $this->assertCount(4, $output['#items']); - $expected_links = ['Llama', '(-) Badger', 'Duck', 'Alpaca']; + $expected_links = [ + $this->buildLinkAssertion('Llama', 10, FALSE, FALSE), + $this->buildLinkAssertion('Badger', 20, TRUE, FALSE), + $this->buildLinkAssertion('Duck', 15, FALSE, FALSE), + $this->buildLinkAssertion('Alpaca', 9, FALSE, FALSE), + ]; foreach ($expected_links as $index => $value) { $this->assertInternalType('array', $output['#items'][$index]); $this->assertEquals($value, $output['#items'][$index]['#title']); @@ -144,10 +156,10 @@ class LinksWidgetTest extends UnitTestCase { $this->assertCount(4, $output['#items']); $expected_links = [ - 'Llama (10)', - '(-) Badger (20)', - 'Duck (15)', - 'Alpaca (9)', + $this->buildLinkAssertion('Llama', 10), + $this->buildLinkAssertion('Badger', 20, TRUE), + $this->buildLinkAssertion('Duck', 15), + $this->buildLinkAssertion('Alpaca', 9), ]; foreach ($expected_links as $index => $value) { $this->assertInternalType('array', $output['#items'][$index]); @@ -180,10 +192,10 @@ class LinksWidgetTest extends UnitTestCase { $this->assertCount(4, $output['#items']); $expected_links = [ - 'Llama (10)', - '(-) Badger (20)', - 'Duck (15)', - 'Alpaca (9)', + $this->buildLinkAssertion('Llama', 10), + $this->buildLinkAssertion('Badger', 20, TRUE), + $this->buildLinkAssertion('Duck', 15), + $this->buildLinkAssertion('Alpaca', 9), ]; foreach ($expected_links as $index => $value) { $this->assertInternalType('array', $output['#items'][$index]); @@ -200,4 +212,29 @@ class LinksWidgetTest extends UnitTestCase { } + /** + * Build a formattable markup object to use in the other tests. + * + * @param $text + * Text to display. + * @param int $count + * Number of results. + * @param bool $active + * Link is active. + * @param bool $show_numbers + * Numbers are displayed. + * + * @return \Drupal\Component\Render\FormattableMarkup + * Formattable markup object for link. + */ + private function buildLinkAssertion($text, $count = 0, $active = FALSE, $show_numbers = TRUE) { + $text = new FormattableMarkup('@text', ['@text' => $text, '@count' => $count]); + if ($show_numbers !== FALSE) { + $text->string .= ' (@count)'; + } + if ($active) { + $text->string = '(-) ' . $text->string; + } + return $text; + } }