From 57b93ed8064800dd047a19f474e11e9589be47f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 21 Jan 2023 22:54:18 +0100 Subject: [PATCH 1/3] fix PersistencePostTest test --- src/Persistence/Post.php | 15 ++++----------- tests/PersistencePostTest.php | 11 +++++++---- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/Persistence/Post.php b/src/Persistence/Post.php index 131092c023..0731224145 100644 --- a/src/Persistence/Post.php +++ b/src/Persistence/Post.php @@ -9,26 +9,19 @@ class Post extends Persistence { - public function load(Model $model, $id = 0): array + public function load(Model $model, $id): array { // carefully copy stuff from $_POST into the model - $data = []; + $dataRaw = [$model->idField => $id]; foreach ($model->getFields() as $field => $def) { - if ($def->type === 'boolean') { - $data[$field] = isset($_POST[$field]); - - continue; - } - if (isset($_POST[$field])) { - $data[$field] = $_POST[$field]; + $dataRaw[$field] = $_POST[$field]; } } - // TODO typecast! + $data = $this->typecastLoadRow($model, $dataRaw); -// return array_merge($model->get(), $data); return $data; } } diff --git a/tests/PersistencePostTest.php b/tests/PersistencePostTest.php index 99a620b3b1..00d1ee6dd0 100644 --- a/tests/PersistencePostTest.php +++ b/tests/PersistencePostTest.php @@ -17,11 +17,13 @@ protected function setUp(): void { parent::setUp(); - $_POST = ['name' => 'John', 'is_married' => 'Y']; + $_POST = ['name' => 'John', 'is_enabled' => '1', 'is_admin' => '0']; + $this->model = new Model(); $this->model->addField('name'); $this->model->addField('surname', ['default' => 'Smith']); - $this->model->addField('is_married', ['type' => 'boolean']); + $this->model->addField('is_enabled', ['type' => 'boolean']); + $this->model->addField('is_admin', ['type' => 'boolean']); } protected function tearDown(): void @@ -41,11 +43,12 @@ public function testPost(): void $m = $this->model; $m->setPersistence($p); - $m = $m->load(0); + $m = $m->load(1); $m->set('surname', 'DefSurname'); static::assertSame('John', $m->get('name')); - static::assertTrue($m->get('is_married')); + static::assertTrue($m->get('is_enabled')); + static::assertFalse($m->get('is_admin')); static::assertSame('DefSurname', $m->get('surname')); } } From 9a8444dc00142949eaca162c18d878512106d783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 21 Jan 2023 22:55:20 +0100 Subject: [PATCH 2/3] Drop Persistence\Post class --- src/Persistence/Post.php | 27 ------------------ tests/PersistencePostTest.php | 54 ----------------------------------- 2 files changed, 81 deletions(-) delete mode 100644 src/Persistence/Post.php delete mode 100644 tests/PersistencePostTest.php diff --git a/src/Persistence/Post.php b/src/Persistence/Post.php deleted file mode 100644 index 0731224145..0000000000 --- a/src/Persistence/Post.php +++ /dev/null @@ -1,27 +0,0 @@ -idField => $id]; - - foreach ($model->getFields() as $field => $def) { - if (isset($_POST[$field])) { - $dataRaw[$field] = $_POST[$field]; - } - } - - $data = $this->typecastLoadRow($model, $dataRaw); - - return $data; - } -} diff --git a/tests/PersistencePostTest.php b/tests/PersistencePostTest.php deleted file mode 100644 index 00d1ee6dd0..0000000000 --- a/tests/PersistencePostTest.php +++ /dev/null @@ -1,54 +0,0 @@ - 'John', 'is_enabled' => '1', 'is_admin' => '0']; - - $this->model = new Model(); - $this->model->addField('name'); - $this->model->addField('surname', ['default' => 'Smith']); - $this->model->addField('is_enabled', ['type' => 'boolean']); - $this->model->addField('is_admin', ['type' => 'boolean']); - } - - protected function tearDown(): void - { - unset($_POST); - - parent::tearDown(); - } - - /** - * Test loading from POST persistence, some type mapping applies. - */ - public function testPost(): void - { - $p = new PostPersistence(); - - $m = $this->model; - $m->setPersistence($p); - - $m = $m->load(1); - $m->set('surname', 'DefSurname'); - - static::assertSame('John', $m->get('name')); - static::assertTrue($m->get('is_enabled')); - static::assertFalse($m->get('is_admin')); - static::assertSame('DefSurname', $m->get('surname')); - } -} From 48dafc77e33b654974e43a438ee378335dbcbf6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 22 Jan 2023 00:54:35 +0100 Subject: [PATCH 3/3] add some HtmlTemplate tests --- src/HtmlTemplate.php | 6 +++++- tests/HtmlTemplateTest.php | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/HtmlTemplate.php b/src/HtmlTemplate.php index e18045c7b0..cca6c4a669 100644 --- a/src/HtmlTemplate.php +++ b/src/HtmlTemplate.php @@ -114,7 +114,11 @@ public function cloneRegion(string $tag): self protected function _unsetFromTagTree(TagTree $tagTree, int $k): void { \Closure::bind(function () use ($tagTree, $k) { - unset($tagTree->children[$k]); + if ($k === array_key_last($tagTree->children)) { + array_pop($tagTree->children); + } else { + unset($tagTree->children[$k]); + } }, null, TagTree::class)(); } diff --git a/tests/HtmlTemplateTest.php b/tests/HtmlTemplateTest.php index 641d80b7c7..79c509dbdc 100644 --- a/tests/HtmlTemplateTest.php +++ b/tests/HtmlTemplateTest.php @@ -78,11 +78,21 @@ public function testHasTag(): void static::assertFalse($t->hasTag(['foo', 'bar', 'non_existent_tag'])); } - public function testSetBadTypeException(): void + public function testSetInvalidUtf8Exception(): void { $t = new HtmlTemplate('{foo}hello{/} guys'); $this->expectException(Exception::class); + $this->expectExceptionMessage('Value is not valid UTF-8'); + $t->set('foo', "\xc2"); + } + + public function testSetNonScalarException(): void + { + $t = new HtmlTemplate('{foo}hello{/} guys'); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Value must be scalar'); $t->set('foo', new \stdClass()); // @phpstan-ignore-line } @@ -125,6 +135,28 @@ public function testSetAppendDel(): void static::assertSameTemplate('{foo}Hi and welcome my dear and smart{/} guys', $t); } + public function testValueEncoded(): void + { + $t = new HtmlTemplate('{foo}hello{/} guys'); + $tagTreeFoo = $t->getTagTree('foo'); + + static::assertTrue($tagTreeFoo->getChildren()[0]->isEncoded()); + static::assertSame('hello', $tagTreeFoo->getChildren()[0]->getHtml()); + + $t->set('foo', '
'); + static::assertFalse($tagTreeFoo->getChildren()[0]->isEncoded()); + static::assertSame('<br>', $tagTreeFoo->getChildren()[0]->getHtml()); + static::assertSame('
', $tagTreeFoo->getChildren()[0]->getUnencoded()); + + $t->dangerouslyAppendHtml('foo', '
'); + static::assertTrue($tagTreeFoo->getChildren()[1]->isEncoded()); + static::assertSame('
', $tagTreeFoo->getChildren()[1]->getHtml()); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Unencoded value is not available'); + $tagTreeFoo->getChildren()[1]->getUnencoded(); + } + public function testClone(): void { $t = new HtmlTemplate('{foo}{inner}hello{/}{/} guys');