I'm trying to set up a test for my custom module. But I can't seem to place my custom block. I can place system blocks just fine. So I don't know if there's something special with a block I make in my custom module.
If I try a system block, it's fine...
$this->drupalPlaceBlock('system_powered_by_block');
If i try my block, not so much...
$this->drupalPlaceBlock('search_block');
My test code:
<?php
namespace Drupal\Tests\dealers\Functional;
use Drupal\Tests\BrowserTestBase;
class UiPageTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['dealers', 'geocoder', 'field', 'filter', 'block', 'text', 'node'];
/**
* Theme to enable.
*
* @var string
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create an article content type that we will use for testing.
$type = $this->container->get('entity_type.manager')->getStorage('node_type')
->create([
'type' => 'article',
'name' => 'Article',
]);
$type->save();
$this->container->get('router.builder')->rebuild();
$this->drupalPlaceBlock('search_block');
}
/**
* Tests that the reaction rule listing page works.
*/
public function testDealerConfigurationPage() {
$account = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($account);
$this->drupalGet('admin/config/dealers');
$this->assertSession()->statusCodeEquals(200);
}
/**
* Tests that the reaction rule listing page works.
*/
public function testDealerSearchPage() {
$this->drupalGet('');
// Test that there is an empty reaction rule listing.
$this->assertSession()->pageTextContains('Dealer search block');
}
}
My block plugin:
namespace Drupal\dealers\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilder;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Database\Connection;
use Drupal\geocoder\Geocoder;
/**
* Provides a 'SearchBlock' block.
*
* @Block(
* id = "search_block",
* admin_label = @Translation("Dealer search block"),
* category = @Translation("Dealers")
* )
*/
class SearchBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* Form builder.
*
* @var \Drupal\Core\Form\FormBuilder
*/
protected FormBuilder $formBuilder;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected Connection $database;
/**
* Drupal\Core\Logger\LoggerChannelFactoryInterface definition.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected LoggerChannelFactoryInterface $loggerFactory;
/**
* Drupal\Core\Messenger\MessengerInterface definition.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Symfony\Component\HttpFoundation\RequestStack definition.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected RequestStack $requestStack;
/**
* Configuration Factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected ConfigFactory $configFactory;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;
/**
* Geocoder.
*
* @var \Drupal\geocoder\Geocoder
*/
protected Geocoder $geocoder;
/**
* Class constructor.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
ConfigFactory $configFactory,
FormBuilder $formBuilder,
LoggerChannelFactoryInterface $logger_factory,
MessengerInterface $messenger,
RequestStack $request_stack,
Connection $database,
EntityTypeManagerInterface $entityTypeManager,
Geocoder $geocoder
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $configFactory;
$this->formBuilder = $formBuilder;
$this->loggerFactory = $logger_factory;
$this->messenger = $messenger;
$this->requestStack = $request_stack;
$this->database = $database;
$this->entityTypeManager = $entityTypeManager;
$this->geocoder = $geocoder;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.factory'),
$container->get('form_builder'),
$container->get('logger.factory'),
$container->get('messenger'),
$container->get('request_stack'),
$container->get('database'),
$container->get('entity_type.manager'),
$container->get('geocoder')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$build['#theme'] = 'dealers_search_block';
$results = [];
$resultCount = '0';
$query = '';
$message = '';
$form = $this->formBuilder->getForm('Drupal\dealers\Form\DealerSearchForm');
$q['zip'] = $this->requestStack->getCurrentRequest()->query->get('zip');
$q['lat'] = $this->requestStack->getCurrentRequest()->query->get('lat');
$q['lon'] = $this->requestStack->getCurrentRequest()->query->get('lon');
if ($q['zip'] || ($q['lat'] && $q['lon'])) {
$results = $this->getDealerResults($q);
if (!empty($results)) {
$resultCount = count($results);
}
if (!$results) {
if ($q['zip'] != '') {
$message = 'No dealers found for ' . $q['zip'] . '. Please try again.';
}
elseif ($q['lat'] && $q['lon']) {
$message = 'No dealers found for ' . $q['lat'] . '/' . $q['lon'] . '. Please try again.';
}
else {
$message = 'No dealers found. Please try again.';
}
}
}
else {
$message = '';
}
if ($q['zip']) {
$query = $q['zip'];
}
if ($q['lat'] && $q['lon']) {
$query = 'your current location';
}
$build['#content']['resultCount'] = $resultCount;
$build['#content']['query'] = $query;
$build['#content']['form'] = $form;
$build['#content']['results'] = $results;
$build['#content']['message'] = $message;
return $build;
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return 0;
}
}