Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 35bb180

Browse files
committed
simplify tests
1 parent 947aa8d commit 35bb180

19 files changed

+306
-1772
lines changed

src/AbstractBlock.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
{
2525
public const string NAME = 'abstract';
2626

27+
public const array ALLOWED_TAGS = [];
28+
2729
protected function __construct(
2830
public string $id,
2931
public string $type,
@@ -47,7 +49,7 @@ public static function create(array $data, array $allowedTags = []): static
4749

4850
public function sanitize(): string
4951
{
50-
$sanitizer = Sanitizer::create($this->allowedTags ?? []);
52+
$sanitizer = Sanitizer::create($this->getAllowedTags());
5153

5254
return $sanitizer->sanitize($this->toHtml());
5355
}
@@ -58,6 +60,12 @@ public function getName(): string
5860
return static::NAME;
5961
}
6062

63+
#[\Override]
64+
public function getAllowedTags(): array
65+
{
66+
return [...static::ALLOWED_TAGS, ...$this->allowedTags ?? []];
67+
}
68+
6169
#[\Override]
6270
public function getSchema(): array
6371
{

src/BlockFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
/**
3131
* Class BlockFactory.
3232
*
33+
* @internal
34+
*
3335
* @author bernard-ng <bernard@devscast.tech>
3436
*/
3537
abstract class BlockFactory

src/BlockInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ public function getName(): string;
1616
public function toHtml(): string;
1717

1818
public function getSchema(): array;
19+
20+
public function getAllowedTags(): array;
1921
}

src/Blocks/Paragraph.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*
2222
* Basic text Tool for the Editor.js.
2323
*
24+
* @todo Support footnotes-tunes
25+
*
2426
* @see https://github.com/editor-js/paragraph
2527
* @see https://github.com/editor-js/footnotes-tune
2628
* @see https://raw.githubusercontent.com/devscast/editorjs-sanitizer/refs/heads/main/schemas/paragraph.schema.json
@@ -31,6 +33,17 @@
3133
{
3234
public const string NAME = 'paragraph';
3335

36+
public const array ALLOWED_TAGS = [
37+
'a' => ['href', 'title', 'rel'],
38+
'span' => ['class'],
39+
'strong' => [],
40+
'em' => [],
41+
'u' => [],
42+
'br' => [],
43+
'p' => ['id'],
44+
'sup' => ['data-tune'],
45+
];
46+
3447
protected function __construct(string $id, string $type, array $data, ?array $tunes = [], ?array $allowedTags = null)
3548
{
3649
Assert::eq($type, self::NAME);

src/Editor.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
final readonly class Editor
2424
{
25+
/**
26+
* @var AbstractBlock[]
27+
*/
2528
public array $blocks;
2629

2730
/**
@@ -40,4 +43,15 @@ public function getBlocks(): array
4043
{
4144
return $this->blocks;
4245
}
46+
47+
public function getHtml(): string
48+
{
49+
$content = '';
50+
51+
foreach ($this->blocks as $block) {
52+
$content .= $block->sanitize();
53+
}
54+
55+
return $content;
56+
}
4357
}

src/Sanitizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
/**
2020
* Class Sanitizer.
2121
*
22+
* @internal
23+
*
2224
* @author bernard-ng <bernard@devscast.tech>
2325
*/
2426
final readonly class Sanitizer

tests/Blocks/AbstractBlockTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Devscast\EditorJs\Tests\Blocks;
6+
7+
use Devscast\EditorJs\AbstractBlock;
8+
use Devscast\EditorJs\Editor;
9+
use Devscast\EditorJs\Exception\EditorException;
10+
use Generator;
11+
use PHPUnit\Framework\Attributes\DataProvider;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Class AbstractBlockTest.
16+
*
17+
* @author bernard-ng <bernard@devscast.tech>
18+
*/
19+
abstract class AbstractBlockTest extends TestCase
20+
{
21+
abstract public static function getValidSchemasProvider(): Generator;
22+
23+
abstract public static function getInvalidSchemasProvider(): Generator;
24+
25+
#[DataProvider('getInvalidSchemasProvider')]
26+
public function testBlockParsingWithInvalidSchema(string $data, string $message): void
27+
{
28+
$this->expectException(EditorException::class);
29+
$editor = new Editor($data);
30+
}
31+
32+
#[DataProvider('getValidSchemasProvider')]
33+
public function testBlockParsingWithValidSchema(string $data, string $expected, string $message): void
34+
{
35+
$this->assertNotEmpty($data);
36+
37+
$editor = new Editor($data);
38+
$this->assertNotEmpty($editor->getBlocks());
39+
$this->assertContainsOnlyInstancesOf(AbstractBlock::class, $editor->getBlocks());
40+
$this->assertSame($expected, $editor->getHtml(), $message);
41+
}
42+
}

tests/Blocks/AttachesTest.php

Lines changed: 7 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -4,167 +4,17 @@
44

55
namespace Devscast\EditorJs\Tests\Blocks;
66

7-
use Devscast\EditorJs\BlockFactory;
8-
use Devscast\EditorJs\Blocks\Attaches;
9-
use Devscast\EditorJs\Exception\EditorException;
10-
use PHPUnit\Framework\TestCase;
11-
12-
class AttachesTest extends TestCase
7+
class AttachesTest extends AbstractBlockTest
138
{
14-
private const array VALID_DATA = [
15-
'file' => [
16-
'url' => 'https://example.com/file.pdf',
17-
'size' => 1024,
18-
'name' => 'document.pdf',
19-
'extension' => 'pdf',
20-
],
21-
'title' => 'Important Document',
22-
];
23-
24-
public function testValidConstruction(): void
25-
{
26-
$attaches = new Attaches(
27-
id: 'test123',
28-
type: 'attaches',
29-
data: self::VALID_DATA
30-
);
31-
32-
$this->assertInstanceOf(Attaches::class, $attaches);
33-
}
34-
35-
public function testInvalidTypeConstruction(): void
36-
{
37-
$this->expectException(EditorException::class);
38-
39-
new Attaches(
40-
id: 'test123',
41-
type: 'invalid-type',
42-
data: self::VALID_DATA
43-
);
44-
}
45-
46-
public function testMissingFileDataConstruction(): void
47-
{
48-
$this->expectException(EditorException::class);
49-
50-
$invalidData = self::VALID_DATA;
51-
unset($invalidData['file']);
52-
53-
new Attaches(
54-
id: 'test123',
55-
type: 'attaches',
56-
data: $invalidData
57-
);
58-
}
59-
60-
public function testMissingFilePropertiesConstruction(): void
61-
{
62-
$testCases = [
63-
'missing url' => 'url',
64-
'missing size' => 'size',
65-
'missing name' => 'name',
66-
'missing extension' => 'extension',
67-
];
68-
69-
foreach ($testCases as $case => $property) {
70-
$this->expectException(EditorException::class);
71-
// dd($case, $property);
72-
$invalidData = self::VALID_DATA;
73-
unset($invalidData['file'][$property]);
74-
new Attaches(
75-
id: 'test123',
76-
type: 'attaches',
77-
data: $invalidData
78-
);
79-
}
80-
}
81-
82-
public function testMissingTitleConstruction(): void
83-
{
84-
$this->expectException(EditorException::class);
85-
$invalidData = self::VALID_DATA;
86-
unset($invalidData['title']);
87-
new Attaches(
88-
id: 'test123',
89-
type: 'attaches',
90-
data: $invalidData
91-
);
92-
}
93-
94-
public function testToHtml(): void
9+
#[\Override]
10+
public static function getValidSchemasProvider(): \Generator
9511
{
96-
$attaches = new Attaches(
97-
id: 'test123',
98-
type: 'attaches',
99-
data: self::VALID_DATA
100-
);
101-
$html = $attaches->toHtml();
102-
$this->assertStringContainsString('<div id="test123">', $html);
103-
$this->assertStringContainsString(
104-
'href="https://example.com/file.pdf"',
105-
$html
106-
);
107-
$this->assertStringContainsString(
108-
'title="Important Document"',
109-
$html
110-
);
111-
$this->assertStringContainsString(
112-
'<strong>document.pdf</strong> (1024 bytes)',
113-
$html
114-
);
12+
yield [];
11513
}
11614

117-
public function testFactoryCreation(): void
15+
#[\Override]
16+
public static function getInvalidSchemasProvider(): \Generator
11817
{
119-
$data = <<<JSON
120-
{
121-
"time": 1739980616433,
122-
"blocks": [
123-
{
124-
"id": "XKNT99-qqS",
125-
"type": "attaches",
126-
"data": {
127-
"file": {
128-
"url": "https://drive.google.com/user/catalog/my-file.pdf",
129-
"size": 12902,
130-
"name": "file.pdf",
131-
"extension": "pdf"
132-
},
133-
"title": "My file"
134-
}
135-
}
136-
]
137-
}
138-
JSON;
139-
140-
$blocks = BlockFactory::parse($data, ['attaches']);
141-
$this->assertCount(1, $blocks);
142-
$this->assertInstanceOf(Attaches::class, $blocks[0]);
143-
}
144-
145-
public function testFactoryCreationWithInvalidData(): void
146-
{
147-
$this->expectException(EditorException::class);
148-
149-
$data = <<<JSON
150-
{
151-
"time": 1739980616433,
152-
"blocks": [
153-
{
154-
"id": "XKNT99-qqS",
155-
"type": "attaches",
156-
"data": {
157-
"file": {
158-
"url": "https://drive.google.com/user/catalog/my-file.pdf",
159-
"size": 12902
160-
},
161-
"title": "My file"
162-
}
163-
}
164-
]
165-
}
166-
JSON;
167-
168-
BlockFactory::parse($data, ['attaches']);
18+
yield [];
16919
}
17020
}

0 commit comments

Comments
 (0)