Skip to content

Commit b867cb1

Browse files
authored
スラッグが設定されている記事にNOでアクセスした場合はリダイレクト fix #3946
1 parent db42022 commit b867cb1

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Cake\Datasource\EntityInterface;
2828
use Cake\Datasource\Paging\PaginatedResultSet;
2929
use Cake\Http\Exception\NotFoundException;
30+
use Cake\Http\Exception\RedirectException;
3031
use Cake\Http\ServerRequest;
3132
use BaserCore\Annotation\UnitTest;
3233
use BaserCore\Annotation\NoTodo;
@@ -372,6 +373,12 @@ public function getViewVarsForSingle(ServerRequest $request, EntityInterface $bl
372373
$post->blog_content_id,
373374
$post->id
374375
] : '';
376+
377+
// スラッグが設定されている記事にNOでアクセスした場合はリダイレクト
378+
if ($post->name && $post->name !== $no) {
379+
$postUrl = $this->BlogPostsService->getUrl($post->blog_content->content, $post, true);
380+
throw new RedirectException($postUrl);
381+
}
375382
}
376383

377384
// ナビゲーションを設定

plugins/bc-blog/tests/TestCase/Service/Front/BlogFrontServiceTest.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,15 @@ public function test_getViewVarsForSingle()
298298
'title' => 'blog post title',
299299
'status' => true
300300
])->persist();
301+
BlogPostFactory::make([
302+
'id' => 2,
303+
'blog_content_id' => 1,
304+
'no' => 2,
305+
'name' => 'slug-test',
306+
'title' => 'blog post title2',
307+
'blog_category_id' => BlogPostFactory::get(1)->get('blog_category_id'),
308+
'status' => true
309+
])->persist();
301310
BlogCategoryFactory::make([
302311
'id' => BlogPostFactory::get(1)->get('blog_category_id'),
303312
'blog_content_id' => 1,
@@ -348,13 +357,38 @@ public function test_getViewVarsForSingle()
348357
$this->assertEquals($rs['crumbs'], $crumbsExpected);
349358

350359

351-
//$noが存在しない場合、
352-
$this->expectException('Cake\Http\Exception\NotFoundException');
353-
$this->BlogFrontService->getViewVarsForSingle(
354-
$this->getRequest(),
360+
// $noが存在しない場合
361+
try {
362+
$this->BlogFrontService->getViewVarsForSingle(
363+
$this->getRequest(),
364+
$BlogContentsService->get(1),
365+
['blog', 'test']
366+
);
367+
$this->fail();
368+
} catch (\Exception $e) {
369+
$this->assertSame('Cake\Http\Exception\NotFoundException', get_class($e));
370+
}
371+
372+
// スラッグが設定されている記事にスラッグでアクセス
373+
$result = $this->BlogFrontService->getViewVarsForSingle(
374+
$request->withParam('pass', ['slug-test']),
355375
$BlogContentsService->get(1),
356376
['blog', 'test']
357377
);
378+
$this->assertEquals($result['post']->title, 'blog post title2');
379+
380+
// スラッグが設定されている記事にNOでアクセス
381+
try {
382+
$this->BlogFrontService->getViewVarsForSingle(
383+
$request->withParam('pass', ['2']),
384+
$BlogContentsService->get(1),
385+
['blog', 'test']
386+
);
387+
$this->fail();
388+
} catch (\Exception $e) {
389+
$this->assertEquals('https://localhost/archives/slug-test', $e->getMessage());
390+
$this->assertSame('Cake\Http\Exception\RedirectException', get_class($e));
391+
}
358392
}
359393

360394
/**

0 commit comments

Comments
 (0)