View source
<?php
namespace Drupal\Tests\facets\Functional;
use Drupal\facets\Entity\Facet;
trait BlockTestTrait {
protected $blocks;
protected function createFacet($name, $id, $field = 'type', $display_id = 'page_1', $source = 'views_page__search_api_test_view', $allowBlockCreation = TRUE) {
$facet_source = "search_api:{$source}__{$display_id}";
$facet = Facet::create([
'id' => $id,
'name' => $name,
'weight' => 0,
]);
$facet
->setFacetSourceId($facet_source);
$facet
->setFieldIdentifier($field);
$facet
->setUrlAlias($id);
$facet
->setWidget('links', [
'show_numbers' => TRUE,
]);
$facet
->addProcessor([
'processor_id' => 'url_processor_handler',
'weights' => [
'pre_query' => -10,
'build' => -10,
],
'settings' => [],
]);
$facet
->setEmptyBehavior([
'behavior' => 'none',
]);
$facet
->setOnlyVisibleWhenFacetSourceIsVisible(TRUE);
$facet
->save();
if ($allowBlockCreation) {
$this->blocks[$id] = $this
->createBlock($id);
}
}
protected function createBlock($id) {
$block = [
'region' => 'footer',
'id' => str_replace('_', '-', $id),
];
return $this
->drupalPlaceBlock('facet_block:' . $id, $block);
}
protected function deleteBlock($id) {
$delete_link_title = \Drupal::VERSION >= 8.300000000000001 ? 'Remove block' : 'Delete';
$delete_confirm_form_button_title = \Drupal::VERSION >= 8.300000000000001 ? 'Remove' : 'Delete';
$orig_success_message = \Drupal::VERSION >= 8.300000000000001 ? 'The block ' . $this->blocks[$id]
->label() . ' has been removed.' : 'The block ' . $this->blocks[$id]
->label() . ' has been deleted.';
$this
->drupalGet('admin/structure/block/manage/' . $this->blocks[$id]
->id(), [
'query' => [
'destination' => 'admin',
],
]);
$this
->clickLink($delete_link_title);
$this
->drupalPostForm(NULL, [], $delete_confirm_form_button_title);
$this
->assertSession()
->pageTextContains($orig_success_message);
}
}