@@ -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