Skip to content

Commit b4e360d

Browse files
authored
ブログでスラッグを使用するとリダイレクトループになる fix #4149
1 parent a051a7b commit b4e360d

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

plugins/bc-blog/src/Model/Table/BlogPostsTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ public function getPublishByNo(int $blogContentId, mixed $no, bool $preview = fa
842842
} else {
843843
$conditions = array_merge_recursive(
844844
$conditions,
845-
['BlogPosts.name' => rawurldecode($no)]
845+
['BlogPosts.name' => $no]
846846
);
847847
}
848848
$entity = $this->find()->where($conditions)

plugins/bc-blog/src/Service/Front/BlogFrontService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ public function getViewVarsForSingle(ServerRequest $request, EntityInterface $bl
355355
{
356356
$isPreview = (bool)$request->getQuery('preview');
357357
$no = $request->getParam('pass.0');
358+
if (is_string($no)) {
359+
$no = rawurldecode($no);
360+
}
358361
$post = $editLink = null;
359362
if($isPreview) {
360363
if($no) {

plugins/bc-blog/tests/Scenario/MultiSiteBlogPostScenario.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected function createBlogPosts()
118118
'id' => 1,
119119
'user_id' => 1,
120120
'blog_content_id' => 6,
121-
'blog_category_id' => 6,
121+
'blog_category_id' => null,
122122
'no' => 3,
123123
'name' => 'release',
124124
'title' => 'プレスリリース',
@@ -172,6 +172,26 @@ protected function createBlogPosts()
172172
'status' => 1,
173173
'exclude_search' => 0,
174174
])->persist();
175+
BlogPostFactory::make([
176+
'id' => 7,
177+
'blog_content_id' => 6,
178+
'blog_category_id' => null,
179+
'no' => 4,
180+
'name' => '日本語スラッグ',
181+
'title' => '日本語スラッグ記事タイトル',
182+
'status' => 1,
183+
'exclude_search' => 0,
184+
])->persist();
185+
BlogPostFactory::make([
186+
'id' => 8,
187+
'blog_content_id' => 6,
188+
'blog_category_id' => null,
189+
'no' => 5,
190+
'name' => null,
191+
'title' => 'スラッグがない記事',
192+
'status' => 1,
193+
'exclude_search' => 0,
194+
])->persist();
175195
return null;
176196
}
177197

plugins/bc-blog/tests/TestCase/Controller/BlogControllerTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use BcBlog\Test\Factory\BlogPostBlogTagFactory;
2828
use BcBlog\Test\Factory\BlogPostFactory;
2929
use BcBlog\Test\Factory\BlogTagFactory;
30+
use BcBlog\Test\Scenario\MultiSiteBlogPostScenario;
3031
use Cake\Datasource\Exception\RecordNotFoundException;
3132
use Cake\Event\Event;
3233
use Cake\TestSuite\IntegrationTestTrait;
@@ -226,12 +227,27 @@ public function test_archives()
226227
*/
227228
public function test_posts()
228229
{
229-
$this->markTestIncomplete('このテストはまだ実装されていません。');
230-
//準備
230+
$this->loadFixtureScenario(MultiSiteBlogPostScenario::class);
231231

232-
//正常系実行
232+
// スラッグが設定されている記事
233+
$this->get('/news/archives/release');
234+
$this->assertResponseOk();
235+
$this->get('/news/archives/3');
236+
$this->assertRedirect('/news/archives/release');
233237

234-
//異常系実行
238+
// 日本語のスラッグが設定されている記事
239+
$this->get('/news/archives/' . rawurlencode('日本語スラッグ'));
240+
$this->assertResponseOk();
241+
$this->get('/news/archives/4');
242+
$this->assertRedirect('/news/archives/' . rawurlencode('日本語スラッグ'));
243+
244+
// スラッグが設定されていない記事
245+
$this->get('/news/archives/5');
246+
$this->assertResponseOk();
247+
248+
// 404
249+
$this->get('/news/archives/9999');
250+
$this->assertResponseCode(404);
235251

236252

237253
}

plugins/bc-blog/tests/TestCase/Model/BlogPostsTableTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,6 @@ public function test_getPublishByNo()
734734
$this->assertEquals($rs->title, 'プレスリリース');
735735
$this->assertNotNull($rs->blog_comments);
736736
$this->assertNotNull($rs->blog_tags);
737-
$this->assertNotNull($rs->blog_category);
738737
$this->assertNotNull($rs->user);
739738
$this->assertNotNull($rs->blog_content);
740739
$this->assertNotNull($rs->blog_content->content);
@@ -746,6 +745,12 @@ public function test_getPublishByNo()
746745
$this->assertEquals($rs->no, 4);
747746
$this->assertEquals($rs->title, 'スマホサイトリリース');
748747

748+
// 日本語スラッグ
749+
$rs = $this->BlogPostsTable->getPublishByNo(6, '日本語スラッグ', true);
750+
$this->assertEquals($rs->title, '日本語スラッグ記事タイトル');
751+
$rs = $this->BlogPostsTable->getPublishByNo(6, '日本語スラッグ', false);
752+
$this->assertEquals($rs->title, '日本語スラッグ記事タイトル');
753+
749754
//サービスクラス
750755
$blogPostsService = $this->getService(BlogPostsServiceInterface::class);
751756
//非公開を設定する

0 commit comments

Comments
 (0)