|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Tests\Core\Search\Common\FieldValueMapper; |
| 10 | + |
| 11 | +use Ibexa\Contracts\Core\Search\Field; |
| 12 | +use Ibexa\Contracts\Core\Search\FieldType\StringField; |
| 13 | +use Ibexa\Core\Search\Common\FieldValueMapper\StringMapper; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * @covers \Ibexa\Core\Search\Common\FieldValueMapper\StringMapper |
| 18 | + */ |
| 19 | +final class StringMapperTest extends TestCase |
| 20 | +{ |
| 21 | + private StringMapper $mapper; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + $this->mapper = new StringMapper(); |
| 26 | + } |
| 27 | + |
| 28 | + public function testCanMap(): void |
| 29 | + { |
| 30 | + $field = $this->createFieldWithValue('hello', new StringField()); |
| 31 | + |
| 32 | + self::assertTrue($this->mapper->canMap($field)); |
| 33 | + } |
| 34 | + |
| 35 | + public function testMapsPlainString(): void |
| 36 | + { |
| 37 | + $field = $this->createFieldWithValue('hello world', new StringField()); |
| 38 | + |
| 39 | + self::assertSame('hello world', $this->mapper->map($field)); |
| 40 | + } |
| 41 | + |
| 42 | + public function testStripsNonPrintableCharacters(): void |
| 43 | + { |
| 44 | + $field = $this->createFieldWithValue("hello\x01\x02world", new StringField()); |
| 45 | + |
| 46 | + self::assertSame('helloworld', $this->mapper->map($field)); |
| 47 | + } |
| 48 | + |
| 49 | + public function testReplacesTabAndVerticalWhitespaceWithSpace(): void |
| 50 | + { |
| 51 | + $field = $this->createFieldWithValue("hello\x09world\x0Bfoo", new StringField()); |
| 52 | + |
| 53 | + self::assertSame('hello world foo', $this->mapper->map($field)); |
| 54 | + } |
| 55 | + |
| 56 | + public function testTruncatesToMaxTermLength(): void |
| 57 | + { |
| 58 | + $longValue = str_repeat('a', StringMapper::MAX_TERM_LENGTH + 100); |
| 59 | + $field = $this->createFieldWithValue($longValue, new StringField()); |
| 60 | + $result = $this->mapper->map($field); |
| 61 | + |
| 62 | + self::assertSame(StringMapper::MAX_TERM_LENGTH, strlen($result)); |
| 63 | + } |
| 64 | + |
| 65 | + public function testTruncatesMultibyteStringAtCharacterBoundary(): void |
| 66 | + { |
| 67 | + // Each UTF-8 character here is 3 bytes (€ = E2 82 AC). |
| 68 | + // Fill up to just past the limit so truncation must happen on a char boundary. |
| 69 | + $char = '€'; |
| 70 | + $charBytes = strlen($char); |
| 71 | + |
| 72 | + self::assertSame(3, $charBytes); |
| 73 | + |
| 74 | + $count = (int) ceil((StringMapper::MAX_TERM_LENGTH + $charBytes) / $charBytes); |
| 75 | + $longValue = str_repeat($char, $count); |
| 76 | + |
| 77 | + $field = $this->createFieldWithValue($longValue, new StringField()); |
| 78 | + $result = $this->mapper->map($field); |
| 79 | + |
| 80 | + self::assertSame(StringMapper::MAX_TERM_LENGTH, strlen($result)); |
| 81 | + // Result must be valid UTF-8 (no split mid-character). |
| 82 | + self::assertSame(1, preg_match('//u', $result)); |
| 83 | + } |
| 84 | + |
| 85 | + public function testValueWithinLimitIsNotTruncated(): void |
| 86 | + { |
| 87 | + $value = str_repeat('a', StringMapper::MAX_TERM_LENGTH); |
| 88 | + $field = $this->createFieldWithValue($value, new StringField()); |
| 89 | + |
| 90 | + self::assertSame($value, $this->mapper->map($field)); |
| 91 | + } |
| 92 | + |
| 93 | + private function createFieldWithValue(string $value, StringField $type): Field |
| 94 | + { |
| 95 | + $field = $this->createMock(Field::class); |
| 96 | + $field |
| 97 | + ->method('getValue') |
| 98 | + ->willReturn($value); |
| 99 | + $field |
| 100 | + ->method('getType') |
| 101 | + ->willReturn($type); |
| 102 | + |
| 103 | + return $field; |
| 104 | + } |
| 105 | +} |
0 commit comments