Commit 5291e210 authored by StryKaizer's avatar StryKaizer Committed by StryKaizer
Browse files

Issue #2713137 by borisson_, StryKaizer: Links widget should provide themable items

parent d8b3ba4b
......@@ -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 <span class="facet-count">(19)</span>');
$this->assertRaw('article <span class="facet-count">(10)</span>');
// Verify that the label is correct for a clicked link.
$this->clickLink('page (19)');
$this->assertLink('(-) page (19)');
$this->clickLinkPartialName('page');
$this->assertRaw('<span class="facet-deactivate">(-)</span> page <span class="facet-count">(19)</span>');
// 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 <span class="facet-count">(9)</span>');
$this->assertRaw('March 2016 <span class="facet-count">(10)</span>');
$this->assertRaw('April 2016 <span class="facet-count">(10)</span>');
$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 <span class="facet-count">(1)</span>');
$this->assertRaw('March 9, 2016 <span class="facet-count">(2)</span>');
$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 <span class="facet-count">(1)</span>');
$this->assertRaw('12 PM <span class="facet-count">(1)</span>');
$this->drupalGet('search/node', ['query' => ['keys' => 'test']]);
$this->assertLink('April 2016 (10)');
$this->clickLink('April 2016 (10)');
$this->assertRaw('April 2016 <span class="facet-count">(10)</span>');
$this->clickLinkPartialName('April 2016');
$this->assertResponse(200);
$this->assertLink('April 1, 2016 (1)');
$this->assertLink('April 2, 2016 (1)');
$this->assertRaw('April 1, 2016 <span class="facet-count">(1)</span>');
$this->assertRaw('April 2, 2016 <span class="facet-count">(1)</span>');
}
/**
......@@ -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 <span class="facet-count">(1)</span>');
$this->clickLinkPartialName('December 2016');
$this->assertResponse(200);
$this->assertLink('December 3, 2016 (1)');
$this->clickLink('December 3, 2016 (1)');
$this->assertRaw('December 3, 2016 <span class="facet-count">(1)</span>');
$this->clickLinkPartialName('December 3, 2016');
$this->assertResponse(200);
}
......
......@@ -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 .= ' <span class="facet-count">(@count)</span>';
}
if ($result->isActive()) {
$text = '(-) ' . $text;
$text->string = '<span class="facet-deactivate">(-)</span> ' . $text->string;
}
return $text;
}
......
......@@ -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);
......
......@@ -353,7 +353,7 @@ class IntegrationTest extends WebTestBase {
$this->assertLink('article');
$this->clickLink('item');
$this->assertLink('(-) item');
$this->assertRaw('<span class="facet-deactivate">(-)</span> 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('<span class="facet-deactivate">(-)</span> item');
$this->assertLink('article');
}
......@@ -429,7 +429,7 @@ class IntegrationTest extends WebTestBase {
$this->assertLink('item');
$this->clickLink('item');
$this->assertLink('(-) item');
$this->assertRaw('<span class="facet-deactivate">(-)</span> 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('<span class="facet-deactivate">(-)</span> 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('<span class="facet-deactivate">(-)</span> grape');
$this->assertLink('orange');
$this->clickLink('orange');
$this->assertText('Displaying 3 search results');
$this->assertLink('grape');
$this->assertLink('(-) orange');
$this->assertRaw('<span class="facet-deactivate">(-)</span> orange');
}
/**
......
......@@ -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 <span class="facet-count">(6)');
$this->assertNoText('apple (4)');
$this->assertNoRaw('apple <span class="facet-count">(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('<span class="facet-deactivate">(-)</span> 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', '<span class="facet-deactivate">(-)</span> strawberry');
$form = [
'facet_sorting[active_widget_order][status]' => FALSE,
......
......@@ -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('<span class="facet-deactivate">(-)</span> 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('<span class="facet-deactivate">(-)</span> item');
$this->assertLink('article');
$this->assertUrl($url);
}
......
......@@ -180,7 +180,7 @@ class WidgetIntegrationTest extends WebTestBase {
$this->assertLink('article');
$this->clickLink('item');
$this->assertLink('(-) item');
$this->assertRaw('<span class="facet-deactivate">(-)</span> 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 <span class="facet-count">(3)</span>');
$this->assertRaw('article <span class="facet-count">(2)</span>');
$this->drupalGet($facet_edit_page);
$this->drupalPostForm(NULL, ['widget' => 'links', 'widget_configs[show_numbers]' => FALSE], $this->t('Save'));
......
......@@ -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 .= ' <span class="facet-count">(@count)</span>';
}
if ($active) {
$text->string = '<span class="facet-deactivate">(-)</span> ' . $text->string;
}
return $text;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment