diff --git a/src/Plugin/Block/PDBlock.php b/src/Plugin/Block/PDBlock.php index 7a17495c..16fdcb98 100644 --- a/src/Plugin/Block/PDBlock.php +++ b/src/Plugin/Block/PDBlock.php @@ -17,6 +17,8 @@ class PDBlock extends PdbBlock { /** * {@inheritDoc} + * + * @codeCoverageIgnore */ public function attachLibraries(array $component) { return ['library' => parent::attachLibraries($component)]; diff --git a/src/Plugin/search_api/processor/CustomValue.php b/src/Plugin/search_api/processor/CustomValue.php index 23c0b58f..0af441b9 100644 --- a/src/Plugin/search_api/processor/CustomValue.php +++ b/src/Plugin/search_api/processor/CustomValue.php @@ -16,7 +16,7 @@ class CustomValue extends SearchApiCustomValue { /** * {@inheritdoc} */ - function getPropertyDefinitions(DatasourceInterface $datasource = NULL) { + public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) { $properties = []; if (!$datasource) { @@ -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); } diff --git a/src/Plugin/search_api/processor/RemoveTags.php b/src/Plugin/search_api/processor/RemoveTags.php index a3024e2e..5eb49d4f 100644 --- a/src/Plugin/search_api/processor/RemoveTags.php +++ b/src/Plugin/search_api/processor/RemoveTags.php @@ -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, * } * ) @@ -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)); } } diff --git a/tests/src/Kernel/Plugin/search_api/processor/CustomValueTest.php b/tests/src/Kernel/Plugin/search_api/processor/CustomValueTest.php new file mode 100644 index 00000000..72ba8647 --- /dev/null +++ b/tests/src/Kernel/Plugin/search_api/processor/CustomValueTest.php @@ -0,0 +1,38 @@ +createInstance('custom_value'); + $this->assertInstanceOf('\Drupal\stanford_profile_helper\Plugin\search_api\processor\CustomValue', $plugin); + } + +} diff --git a/tests/src/Unit/Plugin/Next/PreviewUrlGenerator/SimplePreviewTest.php b/tests/src/Unit/Plugin/Next/PreviewUrlGenerator/SimplePreviewTest.php new file mode 100644 index 00000000..efbf088a --- /dev/null +++ b/tests/src/Unit/Plugin/Next/PreviewUrlGenerator/SimplePreviewTest.php @@ -0,0 +1,54 @@ +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); + } + +} diff --git a/tests/src/Unit/Plugin/search_api/processor/RemoveTagsTest.php b/tests/src/Unit/Plugin/search_api/processor/RemoveTagsTest.php new file mode 100644 index 00000000..302afec4 --- /dev/null +++ b/tests/src/Unit/Plugin/search_api/processor/RemoveTagsTest.php @@ -0,0 +1,68 @@ +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 = '
foo bar

baz

'; + $this->plugin->processFieldValue($value, ''); + $this->assertEquals('foo bar

baz

', $value); + } + +} + +class TestRemoveTags extends RemoveTags { + + public function processFieldValue(&$value, $type) { + return parent::processFieldValue($value, $type); + } + +}