Skip to content

Commit

Permalink
Added test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Nov 16, 2023
1 parent f54a767 commit 497d566
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Plugin/Block/PDBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class PDBlock extends PdbBlock {

/**
* {@inheritDoc}
*
* @codeCoverageIgnore
*/
public function attachLibraries(array $component) {
return ['library' => parent::attachLibraries($component)];
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin/search_api/processor/CustomValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CustomValue extends SearchApiCustomValue {
/**
* {@inheritdoc}
*/
function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = [];

if (!$datasource) {
Expand Down Expand Up @@ -56,7 +56,7 @@ public function addFieldValues(ItemInterface $item) {
continue;
}

$field_value = $token->replace($config['value'], $data, ['clear' => TRUE]);
$field_value = trim($token->replace($config['value'], $data, ['clear' => TRUE]));
if ($field_value !== '') {
$field->addValue($field_value);
}
Expand Down
10 changes: 6 additions & 4 deletions src/Plugin/search_api/processor/RemoveTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
use Drupal\search_api\Processor\FieldsProcessorPluginBase;

/**
* Search API processor to remove only the desired html tags.
*
* @SearchApiProcessor(
* id = "remove_tags",
* label = @Translation("Remove Specific HTML Tags"),
* description = @Translation("Similiar to 'strip_tags', but choose only
* which tags to strip from fields."), stages = {
* description = @Translation("Similiar to 'strip_tags', but choose only which tags to strip from fields."),
* stages = {
* "preprocess_index" = 0,
* }
* )
Expand Down Expand Up @@ -70,9 +72,9 @@ protected function processFieldValue(&$value, $type) {
$text = trim(preg_replace('/<!--(.*)-->/Uis', '', $text));

preg_match_all('/<(\w+)/', $text, $tags_matched);
$text_tags = $tags_matched[1];
$text_tags = $tags_matched[1] ?? [];
$keep_tags = array_diff($text_tags, $this->configuration['tags']);
$value = strip_tags($text, $keep_tags);
$value = trim(strip_tags($text, $keep_tags));
}

}
38 changes: 38 additions & 0 deletions tests/src/Kernel/Plugin/search_api/processor/CustomValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Drupal\Tests\stanford_profile_helper\Kernel\Plugin\search_api\processor;

use Drupal\Tests\search_api\Kernel\Processor\CustomValueTest as SearchApiCustomValueTest;

/**
* @coversDefaultClass \Drupal\stanford_profile_helper\Plugin\search_api\processor\CustomValue
*/
class CustomValueTest extends SearchApiCustomValueTest {

/**
* {@inheritdoc}
*/
protected static $modules = [
'user',
'node',
'field',
'search_api',
'search_api_db',
'search_api_test',
'comment',
'text',
'action',
'system',
'stanford_profile_helper',
'rabbit_hole',
'config_pages',
];

public function testPlugin() {
/** @var \Drupal\search_api\Processor\ProcessorPluginManager $plugin_manager */
$plugin_manager = \Drupal::service('plugin.manager.search_api.processor');
$plugin = $plugin_manager->createInstance('custom_value');
$this->assertInstanceOf('\Drupal\stanford_profile_helper\Plugin\search_api\processor\CustomValue', $plugin);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Drupal\Tests\stanford_profile_helper\Unit\Plugin\Next\PreviewUrlGenerator;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\UnroutedUrlAssembler;
use Drupal\next\Entity\NextSiteInterface;
use Drupal\next\PreviewSecretGeneratorInterface;
use Drupal\stanford_profile_helper\Plugin\Next\PreviewUrlGenerator\SimplePreview;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class SimplePreviewTest extends UnitTestCase {

public function testGenerator() {
$container = new ContainerBuilder();
$container->set('current_user', $this->createMock(AccountProxyInterface::class));
$container->set('datetime.time', $this->createMock(TimeInterface::class));
$container->set('next.preview_secret_generator', $this->createMock(PreviewSecretGeneratorInterface::class));
$container->set('entity_type.manager', $this->createMock(EntityTypeManagerInterface::class));
$container->set('path.validator', $this->createMock(PathValidatorInterface::class));
$container->set('unrouted_url_assembler', $this->getUrlAssembler());
\Drupal::setContainer($container);

$plugin = SimplePreview::create($container, [], '', []);

$site = $this->createMock(NextSiteInterface::class);
$site->method('getPreviewUrl')->willReturn('http://example.test/foo/bar');
$site->method('getPreviewSecret')->willReturn('baz');

$entity_url = Url::fromUserInput('/bar/foo');
$entity = $this->createMock(EntityInterface::class);
$entity->method('toUrl')->willReturn($entity_url);

$url = $plugin->generate($site, $entity)->toString();
$this->assertEquals('http://example.test/foo/bar?slug=/bar/foo&secret=baz', $url);
}

protected function getUrlAssembler() {
$request_stack = new RequestStack();
$request_stack->push(new Request());
$path_processor = $this->createMock(OutboundPathProcessorInterface::class);
return new UnroutedUrlAssembler($request_stack, $path_processor);
}

}
68 changes: 68 additions & 0 deletions tests/src/Unit/Plugin/search_api/processor/RemoveTagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Drupal\Tests\stanford_profile_helper\Unit\search_api\processor;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Form\FormState;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Utility\DataTypeHelperInterface;
use Drupal\search_api\Utility\FieldsHelperInterface;
use Drupal\stanford_profile_helper\Plugin\search_api\processor\RemoveTags;
use Drupal\Tests\UnitTestCase;

class RemoveTagsTest extends UnitTestCase {

/**
* @var \Drupal\stanford_profile_helper\Plugin\search_api\processor\RemoveTags
*/
protected $plugin;

protected function setUp(): void {
parent::setUp();
$container = new ContainerBuilder();
$container->set('string_translation', $this->getStringTranslationStub());
$container->set('search_api.fields_helper', $this->createMock(FieldsHelperInterface::class));
$container->set('search_api.data_type_helper', $this->createMock(DataTypeHelperInterface::class));
$container->set('plugin.manager.element_info', $this->createMock(ElementInfoManagerInterface::class));

$this->plugin = TestRemoveTags::create($container, [], '', []);
$index = $this->createMock(IndexInterface::class);
$index->method('getFields')->willReturn([]);
$this->plugin->setIndex($index);
}

public function testFieldProcessorForm() {
$this->assertContains('div', $this->plugin->defaultConfiguration()['tags']);

$form = [];
$form_state = new FormState();
$form = $this->plugin->buildConfigurationForm($form, $form_state);
$this->assertArrayHasKey('tags', $form);
$form['tags']['#parents'] = [];

$form_state->setValue('tags', "foo\n bar \nbaz");
$this->plugin->validateConfigurationForm($form, $form_state);
$this->assertEquals(['foo', 'bar', 'baz'], $form_state->getValue(['tags']));
$this->assertFalse($form_state::hasAnyErrors());

$form_state->setValue('tags', "foo bar\n baz");
$this->plugin->validateConfigurationForm($form, $form_state);
$this->assertTrue($form_state::hasAnyErrors());
}

public function testFieldProcessor() {
$value = '<div>foo bar<p>baz</p></div>';
$this->plugin->processFieldValue($value, '');
$this->assertEquals('foo bar<p>baz</p>', $value);
}

}

class TestRemoveTags extends RemoveTags {

public function processFieldValue(&$value, $type) {
return parent::processFieldValue($value, $type);
}

}

0 comments on commit 497d566

Please sign in to comment.