Skip to content

Commit

Permalink
Merge pull request #316 from localgovdrupal/fix/315-php-phpstan-issues
Browse files Browse the repository at this point in the history
fix: phpstan issues
  • Loading branch information
finnlewis committed Oct 2, 2023
2 parents a71cd6a + 63c443a commit 18f0754
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Drupal\localgov_directories\Constants as Directory;
use Drupal\search_api\IndexInterface as SearchIndexInterface;
use Drupal\search_api\Item\Field as SearchIndexField;
use Drupal\search_api\SearchApiException;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand Down Expand Up @@ -71,8 +72,12 @@ public function setupLocationSearch(FieldConfigInterface $field, SearchIndexInte
return FALSE;
}

$index_datasrc = $index->getDatasource('entity:node');
if (empty($index_datasrc)) {
try {
$index_datasrc = $index->getDatasource('entity:node');
}
catch (SearchApiException $e) {
// Index::getDatasource() throws an exception if the datasource doesn't
// exist yet.
return FALSE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\KernelTests\KernelTestBase;
use Drupal\localgov_directories\Constants as Directory;
use Drupal\search_api\Entity\Index as SearchIndex;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Views;

/**
Expand Down Expand Up @@ -39,7 +40,7 @@ public function testViewUpgrade(): void {
$view = Views::getView(Directory::CHANNEL_VIEW);
$view->setDisplay(Directory::CHANNEL_VIEW_PROXIMITY_SEARCH_DISPLAY);
$display_for_proximity_search = $view->getDisplay();
$has_proximity_search_display = !empty($display_for_proximity_search);
$has_proximity_search_display = $display_for_proximity_search instanceof DisplayPluginBase;
$this->assertTrue($has_proximity_search_display);

$filters = $display_for_proximity_search->getOption('filters');
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigurationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function insertedTitleSortField(FieldConfigInterface $field): void {
*/
public function importConfigEntity(string $entity_type, string $config_path, string $config_filename): bool {
$config_src = new ConfigFileStorage($config_path);
if (empty($config_src)) {
if (!$config_src instanceof ConfigFileStorage) {
return FALSE;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Form/LocalgovDirectoriesFacetsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function save(array $form, FormStateInterface $form_state) {
}

$form_state->setRedirect('entity.localgov_directories_facets.collection');

return $result;
}

}
3 changes: 3 additions & 0 deletions src/Form/LocalgovDirectoriesFacetsTypeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function save(array $form, FormStateInterface $form_state) {
$status = $entity_type->save();

$t_args = ['%name' => $entity_type->label()];
$message = '';
if ($status == SAVED_UPDATED) {
$message = $this->t('The directory facets type %name has been updated.', $t_args);
}
Expand All @@ -95,6 +96,8 @@ public function save(array $form, FormStateInterface $form_state) {
$this->messenger()->addStatus($message);

$form_state->setRedirectUrl($entity_type->toUrl('collection'));

return $status;
}

}
13 changes: 10 additions & 3 deletions tests/src/Functional/ResetFacetFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class ResetFacetFilterTest extends BrowserTestBase {
*/
protected $adminUser;

/**
* Directory Channel Node Id.
*
* @var string|int|null
*/
protected $directoryChannelNodeId;

/**
* Modules to enable.
*
Expand Down Expand Up @@ -71,7 +78,7 @@ protected function setUp(): void {

$directory->save();

$this->directory_channel_node_id = $directory->id();
$this->directoryChannelNodeId = $directory->id();

// Directory pages.
for ($j = 1; $j < 3; $j++) {
Expand Down Expand Up @@ -100,7 +107,7 @@ public function testShowResetFilterLink() {
$this->drupalLogin($this->adminUser);

// Not displaying the reset link..
$this->drupalGet('node/' . $this->directory_channel_node_id);
$this->drupalGet('node/' . $this->directoryChannelNodeId);
$this->assertSession()->ElementNotExists('css', '.facets-reset');

$this->drupalGet('admin/config/search/facets/' . $id . '/edit');
Expand Down Expand Up @@ -129,7 +136,7 @@ public function testShowResetFilterLink() {
$this->assertSession()->checkboxChecked('widget_config[show_reset_link]');

// Now displaying the reset link.
$this->drupalGet('node/' . $this->directory_channel_node_id);
$this->drupalGet('node/' . $this->directoryChannelNodeId);
$this->assertSession()->ElementExists('css', '.facets-reset');
}

Expand Down
63 changes: 42 additions & 21 deletions tests/src/Kernel/EntityReferenceChannelsSelectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ class EntityReferenceChannelsSelectionTest extends KernelTestBase {
*/
protected $selectionHandler;

/**
* Directory nodes for testing.
*
* @var \Drupal\node\Entity\Node[]
*/
protected $directoryNodes;

/**
* Page type for testing.
*
* @var string
*/
protected $pageType;

/**
* Other type for testing.
*
* @var string
*/
protected $otherType;

/**
* {@inheritdoc}
*/
Expand All @@ -79,70 +100,70 @@ protected function setUp(): void {
$node2 = $this->createNode(['type' => 'localgov_directory']);
$node3 = $this->createNode(['type' => 'localgov_directory']);

$this->directory_nodes = [];
$this->directoryNodes = [];
foreach ([$node1, $node2, $node3] as $node) {
$this->directory_nodes[$node->id()] = $node;
$this->directoryNodes[$node->id()] = $node;
}

$this->page_type = strtolower($this->randomMachineName());
NodeType::create(['type' => $this->page_type])->save();
$this->pageType = strtolower($this->randomMachineName());
NodeType::create(['type' => $this->pageType])->save();
$handler_settings = [
'sort' => [
'field' => 'title',
'direction' => 'DESC',
],
];
$this->createEntityReferenceField('node', $this->page_type, 'localgov_directory_channels', $this->randomString(), 'node', 'localgov_directories_channels_selection', $handler_settings);
$this->createEntityReferenceField('node', $this->pageType, 'localgov_directory_channels', $this->randomString(), 'node', 'localgov_directories_channels_selection', $handler_settings);

$this->other_type = strtolower($this->randomMachineName());
NodeType::create(['type' => $this->other_type])->save();
$this->otherType = strtolower($this->randomMachineName());
NodeType::create(['type' => $this->otherType])->save();
$handler_settings = [
'sort' => [
'field' => 'title',
'direction' => 'DESC',
],
];
$this->createEntityReferenceField('node', $this->other_type, 'localgov_directory_channels', $this->randomString(), 'node', 'localgov_directories_channels_selection', $handler_settings);
$this->createEntityReferenceField('node', $this->otherType, 'localgov_directory_channels', $this->randomString(), 'node', 'localgov_directories_channels_selection', $handler_settings);
}

/**
* Tests the selection handler.
*/
public function testSelectionHandler() {
// Check the three directory nodes are returned.
$field_config = FieldConfig::loadByName('node', $this->page_type, 'localgov_directory_channels');
$page = $this->createNode(['type' => $this->page_type]);
$field_config = FieldConfig::loadByName('node', $this->pageType, 'localgov_directory_channels');
$page = $this->createNode(['type' => $this->pageType]);
$this->selectionHandler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field_config, $page);
$selection = $this->selectionHandler->getReferenceableEntities();
foreach ($selection as $node_type => $values) {
foreach ($values as $nid => $label) {
$this->assertSame($node_type, $this->directory_nodes[$nid]->bundle());
$this->assertSame(trim(strip_tags($label)), Html::escape($this->directory_nodes[$nid]->label()));
$this->assertSame($node_type, $this->directoryNodes[$nid]->bundle());
$this->assertSame(trim(strip_tags($label)), Html::escape($this->directoryNodes[$nid]->label()));
}
}

// Remove one directory node and make it only accessible to the other type.
$directory = array_pop($this->directory_nodes);
$directory->localgov_directory_channel_types = [['target_id' => $this->other_type]];
$directory = array_pop($this->directoryNodes);
$directory->localgov_directory_channel_types = [['target_id' => $this->otherType]];
$directory->save();
$selection = $this->selectionHandler->getReferenceableEntities();
foreach ($selection as $node_type => $values) {
foreach ($values as $nid => $label) {
$this->assertSame($node_type, $this->directory_nodes[$nid]->bundle());
$this->assertSame(trim(strip_tags($label)), Html::escape($this->directory_nodes[$nid]->label()));
$this->assertSame($node_type, $this->directoryNodes[$nid]->bundle());
$this->assertSame(trim(strip_tags($label)), Html::escape($this->directoryNodes[$nid]->label()));
}
}

// Check the removed node is accessible to the other type.
$field_config = FieldConfig::loadByName('node', $this->other_type, 'localgov_directory_channels');
$other = $this->createNode(['type' => $this->other_type]);
$field_config = FieldConfig::loadByName('node', $this->otherType, 'localgov_directory_channels');
$other = $this->createNode(['type' => $this->otherType]);
$this->selectionHandler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field_config, $other);
$other_selection = $this->selectionHandler->getReferenceableEntities();
$this->directory_nodes[$directory->id()] = $directory;
$this->directoryNodes[$directory->id()] = $directory;
foreach ($other_selection as $node_type => $values) {
foreach ($values as $nid => $label) {
$this->assertSame($node_type, $this->directory_nodes[$nid]->bundle());
$this->assertSame(trim(strip_tags($label)), Html::escape($this->directory_nodes[$nid]->label()));
$this->assertSame($node_type, $this->directoryNodes[$nid]->bundle());
$this->assertSame(trim(strip_tags($label)), Html::escape($this->directoryNodes[$nid]->label()));
}
}

Expand Down

0 comments on commit 18f0754

Please sign in to comment.