From 09e14329baddb3e265d3974b39486f6e446caa5e Mon Sep 17 00:00:00 2001 From: seto1 <30764014+seto1@users.noreply.github.com> Date: Sat, 29 Jun 2024 15:05:11 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=96=E3=83=AD=E3=82=B0=20=E6=8A=95?= =?UTF-8?q?=E7=A8=BF=E8=80=85=E5=88=A5=E8=A8=98=E4=BA=8B=E4=B8=80=E8=A6=A7?= =?UTF-8?q?URL=E5=A4=89=E6=9B=B4=20(#3562)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bc-blog/src/Controller/BlogController.php | 8 +++--- .../bc-blog/src/Service/BlogPostsService.php | 28 ++----------------- .../src/Service/BlogPostsServiceInterface.php | 4 +-- .../src/Service/Front/BlogFrontService.php | 6 ++-- .../Front/BlogFrontServiceInterface.php | 6 ++-- .../Controller/BlogControllerTest.php | 2 +- .../TestCase/Service/BlogPostsServiceTest.php | 16 ++--------- .../Service/Front/BlogFrontServiceTest.php | 4 +-- .../element/widget/blog_author_archives.php | 14 ++++------ 9 files changed, 25 insertions(+), 63 deletions(-) diff --git a/plugins/bc-blog/src/Controller/BlogController.php b/plugins/bc-blog/src/Controller/BlogController.php index d4c2c4e0fd..d9aaac0194 100755 --- a/plugins/bc-blog/src/Controller/BlogController.php +++ b/plugins/bc-blog/src/Controller/BlogController.php @@ -149,7 +149,7 @@ public function index( * * ### URL例 * - カテゴリ別記事一覧: /news/archives/category/category-name - * - 作成者別記事一覧: /news/archives/author/author-name + * - 作成者別記事一覧: /news/archives/author/user-id * - タグ別記事一覧: /news/archives/tag/tag-name * - 年別記事一覧: /news/archives/date/2022 * - 月別記事一覧: /news/archives/date/2022/12 @@ -198,15 +198,15 @@ public function archives( case 'author': if (count($pass) > 2) $this->notFound(); - $author = isset($pass[1])? $pass[1] : ''; + $userId = isset($pass[1]) ? (int) $pass[1] : ''; $this->set($service->getViewVarsForArchivesByAuthor( - $this->paginate($blogPostsService->getIndexByAuthor($author, array_merge([ + $this->paginate($blogPostsService->getIndexByAuthor($userId, array_merge([ 'status' => 'publish', 'blog_content_id' => $blogContent->id, 'direction' => $blogContent->list_direction, 'draft' => false ], $this->getRequest()->getQueryParams())), ['limit' => $blogContent->list_count]), - $author, + $userId, $blogContent )); break; diff --git a/plugins/bc-blog/src/Service/BlogPostsService.php b/plugins/bc-blog/src/Service/BlogPostsService.php index 80381ad7ba..7335471236 100755 --- a/plugins/bc-blog/src/Service/BlogPostsService.php +++ b/plugins/bc-blog/src/Service/BlogPostsService.php @@ -242,7 +242,6 @@ protected function createIndexConditions(Query $query, array $params) 'site_id' => null, 'category' => null, 'keyword' => null, - 'author' => null, 'tag' => null, 'year' => null, 'month' => null, @@ -342,10 +341,6 @@ protected function createIndexConditions(Query $query, array $params) if ($params['keyword']) { $conditions = $this->createKeywordCondition($conditions, $params['keyword']); } - // 作成者 - if ($params['author']) { - $conditions = $this->createAuthorCondition($conditions, $params['author']); - } return $query->where($conditions); } @@ -538,23 +533,6 @@ public function createYearMonthDayCondition($conditions, $year, $month, $day) return $conditions; } - /** - * 作成者の条件を作成する - * - * @param array $conditions - * @param string $author - * @return array - * @checked - * @noTodo - * @unitTest - */ - public function createAuthorCondition($conditions, $author) - { - $user = $this->BlogPosts->Users->find()->where(['Users.name' => $author])->first(); - $conditions['BlogPosts.user_id'] = $user->id; - return $conditions; - } - /** * 初期データ用のエンティティを取得 * @@ -809,16 +787,16 @@ public function getIndexByCategory($category, array $options = []) /** * 著者別記事一覧を取得 * - * @param string $author + * @param int $userId * @param array $options * @return Query * @checked * @noTodo * @unitTest */ - public function getIndexByAuthor(string $author, array $options = []) + public function getIndexByAuthor(int $userId, array $options = []) { - $options['author'] = $author; + $options['user_id'] = $userId; return $this->getIndex($options); } diff --git a/plugins/bc-blog/src/Service/BlogPostsServiceInterface.php b/plugins/bc-blog/src/Service/BlogPostsServiceInterface.php index aab2f2e32b..6d0161819c 100755 --- a/plugins/bc-blog/src/Service/BlogPostsServiceInterface.php +++ b/plugins/bc-blog/src/Service/BlogPostsServiceInterface.php @@ -200,14 +200,14 @@ public function getIndexByCategory($category, array $options = []); /** * 著者別記事一覧を取得 * - * @param string $author + * @param int $userId * @param array $options * @return Query * @checked * @noTodo * @unitTest */ - public function getIndexByAuthor(string $author, array $options = []); + public function getIndexByAuthor(int $userId, array $options = []); /** * タグ別記事一覧を取得 diff --git a/plugins/bc-blog/src/Service/Front/BlogFrontService.php b/plugins/bc-blog/src/Service/Front/BlogFrontService.php index d567b7a23f..3361bc0cbe 100755 --- a/plugins/bc-blog/src/Service/Front/BlogFrontService.php +++ b/plugins/bc-blog/src/Service/Front/BlogFrontService.php @@ -249,17 +249,17 @@ public function getCategoryCrumbs(string $baseUrl, int $categoryId, $isCategoryP /** * 著者別アーカイブ一覧の view 用変数を取得する * @param ResultSet|PaginatedResultSet $posts - * @param string $author + * @param int $userId * @param BlogContent $blogContent * @return array * @checked * @noTodo * @unitTest */ - public function getViewVarsForArchivesByAuthor(ResultSet|PaginatedResultSet $posts, string $author, BlogContent $blogContent): array + public function getViewVarsForArchivesByAuthor(ResultSet|PaginatedResultSet $posts, int $userId, BlogContent $blogContent): array { $usersTable = TableRegistry::getTableLocator()->get('BaserCore.Users'); - $author = $usersTable->find('available')->where(['Users.name' => $author])->first(); + $author = $usersTable->find('available')->where(['Users.id' => $userId])->first(); if (!$author) { throw new NotFoundException(); } diff --git a/plugins/bc-blog/src/Service/Front/BlogFrontServiceInterface.php b/plugins/bc-blog/src/Service/Front/BlogFrontServiceInterface.php index e28c916e20..4d966c8dab 100755 --- a/plugins/bc-blog/src/Service/Front/BlogFrontServiceInterface.php +++ b/plugins/bc-blog/src/Service/Front/BlogFrontServiceInterface.php @@ -83,14 +83,14 @@ public function getCategoryCrumbs(string $baseUrl, int $categoryId, $isCategoryP /** * 著者別アーカイブ一覧の view 用変数を取得する - * @param ResultSet $posts - * @param string $author + * @param ResultSet|PaginatedResultSet $posts + * @param int $userId * @return array * @checked * @noTodo * @unitTest */ - public function getViewVarsForArchivesByAuthor(ResultSet $posts, string $author, BlogContent $blogContent): array; + public function getViewVarsForArchivesByAuthor(ResultSet|PaginatedResultSet $posts, int $userId, BlogContent $blogContent): array; /** * タグ別アーカイブ一覧の view 用変数を取得する diff --git a/plugins/bc-blog/tests/TestCase/Controller/BlogControllerTest.php b/plugins/bc-blog/tests/TestCase/Controller/BlogControllerTest.php index 8497fa4e71..4d8dae923c 100755 --- a/plugins/bc-blog/tests/TestCase/Controller/BlogControllerTest.php +++ b/plugins/bc-blog/tests/TestCase/Controller/BlogControllerTest.php @@ -195,7 +195,7 @@ public function test_archives() $this->assertEquals('release', $vars['blogCategory']->name); $this->assertEquals('post1', $vars['posts']->toArray()[0]->name); //type = 'author' - $this->get('/news/archives/author/name'); + $this->get('/news/archives/author/1'); $this->assertResponseOk(); $vars = $this->_controller->viewBuilder()->getVars(); $this->assertEquals('author', $vars['blogArchiveType']); diff --git a/plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php b/plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php index fc16f6b76f..2760891501 100755 --- a/plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php +++ b/plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php @@ -411,18 +411,6 @@ public function testCreateYearMonthDayCondition() $this->assertEquals("01", $result['DAY(BlogPosts.posted)']); } - /** - * 作成者の条件を作成する - */ - public function testCreateAuthorCondition() - { - //データ 生成 - UserFactory::make(['id' => 1, 'name' => 'test name', 'email' => 'test_name@gmail.com'])->persist(); - //戻り値を確認 - $result = $this->BlogPostsService->createAuthorCondition([], "test name"); - $this->assertEquals($result["BlogPosts.user_id"], 1); - } - /** * 並び替え設定を生成する */ @@ -965,7 +953,7 @@ public function testGetIndexByAuthor() // サービスメソッドを呼ぶ // test author1 の記事を取得、id昇順 - $result = $this->BlogPostsService->getIndexByAuthor('test author1', [ + $result = $this->BlogPostsService->getIndexByAuthor(2, [ 'direction' => 'ASC', 'order' => 'id', ]); @@ -984,7 +972,7 @@ public function testGetIndexByAuthor() // サービスメソッドを呼ぶ // 記事が存在しない - $result = $this->BlogPostsService->getIndexByAuthor('test author3', []); + $result = $this->BlogPostsService->getIndexByAuthor(4, []); // 戻り値を確認 // 指定した author の記事が存在しない diff --git a/plugins/bc-blog/tests/TestCase/Service/Front/BlogFrontServiceTest.php b/plugins/bc-blog/tests/TestCase/Service/Front/BlogFrontServiceTest.php index c062b48e5d..e5435710a2 100755 --- a/plugins/bc-blog/tests/TestCase/Service/Front/BlogFrontServiceTest.php +++ b/plugins/bc-blog/tests/TestCase/Service/Front/BlogFrontServiceTest.php @@ -511,7 +511,7 @@ public function test_getViewVarsForArchivesByAuthor() // サービスメソッドを呼ぶ $result = $this->BlogFrontService->getViewVarsForArchivesByAuthor( $blogPostsService->getIndex([])->all(), - 'name', + 1, $blogContentsService->get(1) ); @@ -533,7 +533,7 @@ public function test_getViewVarsForArchivesByAuthor() $this->expectException("Cake\Http\Exception\NotFoundException"); $this->BlogFrontService->getViewVarsForArchivesByAuthor( $blogPostsService->getIndex([])->all(), - 'author name test', + 999, $blogContentsService->get(1) ); } diff --git a/plugins/bc-front/templates/plugin/BcBlog/element/widget/blog_author_archives.php b/plugins/bc-front/templates/plugin/BcBlog/element/widget/blog_author_archives.php index 2148aa9dc0..2b8fbb6750 100755 --- a/plugins/bc-front/templates/plugin/BcBlog/element/widget/blog_author_archives.php +++ b/plugins/bc-front/templates/plugin/BcBlog/element/widget/blog_author_archives.php @@ -47,7 +47,7 @@ getRequest()->getPath() === $baseCurrentUrl . $author->name) { + if ($this->getRequest()->getPath() === $baseCurrentUrl . $author->id) { $class[] = 'current'; } if ($view_count) { @@ -57,14 +57,10 @@ } ?>