From 78ff499b020ad02d442848c71a2f995a82591941 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 22 Dec 2024 15:35:01 +0300 Subject: [PATCH 1/4] Fix state leak --- CHANGELOG.md | 1 + composer.json | 2 +- src/ViewRenderer.php | 2 +- .../RenderCombinationsTest.php | 70 +++++++++++++++++++ .../RenderCombinations/layout.php | 18 +++++ .../ViewRenderer/RenderCombinations/view.php | 9 +++ 6 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 tests/ViewRenderer/RenderCombinations/RenderCombinationsTest.php create mode 100644 tests/ViewRenderer/RenderCombinations/layout.php create mode 100644 tests/ViewRenderer/RenderCombinations/view.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 5675a0b..5ccfa60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 7.2.1 under development - Enh #135: Add `CsrfTokenMiddleware` support in `CsrfViewInjection` (@vjik) +- Bug #137: Fix state leak in some combinations of `ViewRenderer` render methods (@vjik) ## 7.2.0 October 02, 2024 diff --git a/composer.json b/composer.json index 0a4abb3..24ac6f2 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "yiisoft/friendly-exception": "^1.0", "yiisoft/html": "^2.5|^3.0", "yiisoft/strings": "^2.0", - "yiisoft/view": "^10|^11" + "yiisoft/view": "dev-master as 11.0.1" }, "require-dev": { "httpsoft/http-message": "^1.0", diff --git a/src/ViewRenderer.php b/src/ViewRenderer.php index 2845f8d..9b35b8c 100644 --- a/src/ViewRenderer.php +++ b/src/ViewRenderer.php @@ -314,7 +314,7 @@ private function renderProxy( array $metaTags, array $linkTags ): string { - $currentView = $this->view->withContext($this); + $currentView = $this->view->deepClone()->withContext($this); if ($this->locale !== null) { $currentView = $currentView->withLocale($this->locale); diff --git a/tests/ViewRenderer/RenderCombinations/RenderCombinationsTest.php b/tests/ViewRenderer/RenderCombinations/RenderCombinationsTest.php new file mode 100644 index 0000000..83fff9e --- /dev/null +++ b/tests/ViewRenderer/RenderCombinations/RenderCombinationsTest.php @@ -0,0 +1,70 @@ + + + + test + + HTML; + + public function testRenderAfterRenderPartial(): void + { + $renderer = $this->createRenderer(); + + $this->assertSame(self::EXPECTED_VIEW, $renderer->renderPartialAsString('view')); + $this->assertSame(self::EXPECTED_CONTENT, (string) $renderer->render('view')->getBody()); + } + + public function testRenderPartialAfterRender(): void + { + $renderer = $this->createRenderer(); + + $this->assertSame(self::EXPECTED_CONTENT, (string) $renderer->render('view')->getBody()); + $this->assertSame(self::EXPECTED_VIEW, $renderer->renderPartialAsString('view')); + } + + public function testRenderTwice(): void + { + $renderer = $this->createRenderer(); + + $this->assertSame(self::EXPECTED_CONTENT, (string) $renderer->render('view')->getBody()); + $this->assertSame(self::EXPECTED_CONTENT, (string) $renderer->render('view')->getBody()); + } + + private function createRenderer(): ViewRenderer + { + return new ViewRenderer( + new DataResponseFactory(new ResponseFactory(), new StreamFactory()), + new Aliases(), + new WebView(), + __DIR__, + __DIR__ . '/layout.php', + [ + new class() implements LinkTagsInjectionInterface { + public function getLinkTags(): array + { + return [Link::toCssFile('style.css')]; + } + } + ] + ); + } +} diff --git a/tests/ViewRenderer/RenderCombinations/layout.php b/tests/ViewRenderer/RenderCombinations/layout.php new file mode 100644 index 0000000..2dfce94 --- /dev/null +++ b/tests/ViewRenderer/RenderCombinations/layout.php @@ -0,0 +1,18 @@ +beginPage(); +?> +head() ?> + + +endBody() ?> + +endPage(); diff --git a/tests/ViewRenderer/RenderCombinations/view.php b/tests/ViewRenderer/RenderCombinations/view.php new file mode 100644 index 0000000..954ae85 --- /dev/null +++ b/tests/ViewRenderer/RenderCombinations/view.php @@ -0,0 +1,9 @@ + Date: Sun, 22 Dec 2024 12:41:16 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- .../RenderCombinations/RenderCombinationsTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ViewRenderer/RenderCombinations/RenderCombinationsTest.php b/tests/ViewRenderer/RenderCombinations/RenderCombinationsTest.php index 83fff9e..ab6f172 100644 --- a/tests/ViewRenderer/RenderCombinations/RenderCombinationsTest.php +++ b/tests/ViewRenderer/RenderCombinations/RenderCombinationsTest.php @@ -58,12 +58,12 @@ private function createRenderer(): ViewRenderer __DIR__, __DIR__ . '/layout.php', [ - new class() implements LinkTagsInjectionInterface { + new class () implements LinkTagsInjectionInterface { public function getLinkTags(): array { return [Link::toCssFile('style.css')]; } - } + }, ] ); } From 1c25f3814f9c494478e3c3e8ff9f9296f24d0c83 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 23 Dec 2024 10:48:58 +0300 Subject: [PATCH 3/4] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 24ac6f2..c957155 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "yiisoft/friendly-exception": "^1.0", "yiisoft/html": "^2.5|^3.0", "yiisoft/strings": "^2.0", - "yiisoft/view": "dev-master as 11.0.1" + "yiisoft/view": "^12" }, "require-dev": { "httpsoft/http-message": "^1.0", From 3ffd9fb53243b494bd9edac601271fb8e58511be Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 23 Dec 2024 10:49:47 +0300 Subject: [PATCH 4/4] Update view.php --- tests/ViewRenderer/RenderCombinations/view.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/ViewRenderer/RenderCombinations/view.php b/tests/ViewRenderer/RenderCombinations/view.php index 954ae85..7f4c005 100644 --- a/tests/ViewRenderer/RenderCombinations/view.php +++ b/tests/ViewRenderer/RenderCombinations/view.php @@ -2,8 +2,4 @@ declare(strict_types=1); -/** - * @var string $name - */ - echo 'test';