diff --git a/plugins/baser-core/src/Utility/BcFileUploader.php b/plugins/baser-core/src/Utility/BcFileUploader.php index 8dc12f8be6..32ab4611bb 100644 --- a/plugins/baser-core/src/Utility/BcFileUploader.php +++ b/plugins/baser-core/src/Utility/BcFileUploader.php @@ -909,17 +909,20 @@ public function getUniqueFileName(array $setting, array $file, EntityInterface $ * 保存先のフォルダを設定し、取得する * @param bool $isTheme * @param bool $limited - * @return string + * @return string|bool * @checked * @noTodo * @unitTest */ - public function getSaveDir(bool $isTheme = false, bool $limited = false): string + public function getSaveDir(bool $isTheme = false, bool $limited = false): bool|string { if (!$isTheme) { $basePath = WWW_ROOT . 'files' . DS; } else { $request = Router::getRequest(); + + if (is_null($request)) return false; + $site = $request->getAttribute('currentSite'); if ($site && $site->theme) { $basePath = ROOT . DS . 'plugins' . DS . $site->theme . DS . 'webroot' . DS . 'files' . DS; diff --git a/plugins/baser-core/src/Utility/BcUtil.php b/plugins/baser-core/src/Utility/BcUtil.php index 6beedf5635..4d2c944857 100644 --- a/plugins/baser-core/src/Utility/BcUtil.php +++ b/plugins/baser-core/src/Utility/BcUtil.php @@ -1175,8 +1175,14 @@ public static function getCurrentTheme() $theme = Inflector::camelize(Inflector::underscore(Configure::read('BcApp.coreFrontTheme'))); if (!BcUtil::isInstalled()) return $theme; $request = Router::getRequest(); - /** @var Site $site */ - $site = $request->getAttribute('currentSite'); + + if (is_null($request)) + $site = null; + else{ + /** @var Site $site */ + $site = $request->getAttribute('currentSite'); + } + if ($site) { if($site->theme) { return $site->theme; diff --git a/plugins/baser-core/src/View/Helper/BcUploadHelper.php b/plugins/baser-core/src/View/Helper/BcUploadHelper.php index ef5dfc05ef..f79973f212 100755 --- a/plugins/baser-core/src/View/Helper/BcUploadHelper.php +++ b/plugins/baser-core/src/View/Helper/BcUploadHelper.php @@ -328,7 +328,7 @@ public function uploadImage($fieldName, $entity, $options = []) $fileUrl = $this->getBasePath($settings);; $fileUrlInTheme = $this->getBasePath($settings, true); $saveDir = $this->table->getSaveDir(false, $options['limited']); - $saveDirInTheme = $this->table->getSaveDir(true, $options['limited']); + $saveDirInTheme = $this->table->getSaveDir(true, $options['limited']) ?? ''; $settingField = $fieldName; if(strpos($fieldName, '.') !== false) { diff --git a/plugins/bc-blog/src/Model/Entity/BlogPost.php b/plugins/bc-blog/src/Model/Entity/BlogPost.php index feb29a268b..ff757a48ca 100755 --- a/plugins/bc-blog/src/Model/Entity/BlogPost.php +++ b/plugins/bc-blog/src/Model/Entity/BlogPost.php @@ -14,8 +14,10 @@ use BaserCore\Annotation\UnitTest; use BaserCore\Annotation\NoTodo; use BaserCore\Annotation\Checked; +use BcBlog\View\Helper\BlogHelper; use Cake\I18n\FrozenTime; use Cake\ORM\Entity; +use Cake\View\View; /** * Class BlogPost @@ -53,4 +55,20 @@ class BlogPost extends Entity 'id' => false ]; + /** + * アイキャッチのフルパス + */ + protected $_virtual = ['_eyecatch']; + + /** + * アイキャッチのフルパスを取得 + * @return string + * @checked + * @noTodo + * @unitTest + */ + protected function _get_eyecatch(){ + $BlogHelper = new BlogHelper(new View()); + return $BlogHelper->getEyeCatch($this, ['output' => 'url']); + } } diff --git a/plugins/bc-blog/src/View/Helper/BlogHelper.php b/plugins/bc-blog/src/View/Helper/BlogHelper.php index 789a812808..d295ec90ac 100755 --- a/plugins/bc-blog/src/View/Helper/BlogHelper.php +++ b/plugins/bc-blog/src/View/Helper/BlogHelper.php @@ -122,6 +122,7 @@ public function __construct(View $view, array $config = []) public function setContent($blogContentId = null) { $blogContentUpdated = false; + $content = false; if (empty($this->currentBlogContent) || ($blogContentId != $this->currentBlogContent->id)) { if ($blogContentId) { if ($this->_View->getRequest()->getQuery('preview') == 'default' && $this->_View->getRequest()->getData()) { diff --git a/plugins/bc-blog/tests/TestCase/Controller/Api/BlogPostsControllerTest.php b/plugins/bc-blog/tests/TestCase/Controller/Api/BlogPostsControllerTest.php index 73573ea4b1..dd162f99ab 100755 --- a/plugins/bc-blog/tests/TestCase/Controller/Api/BlogPostsControllerTest.php +++ b/plugins/bc-blog/tests/TestCase/Controller/Api/BlogPostsControllerTest.php @@ -11,6 +11,7 @@ namespace BcBlog\Test\TestCase\Controller\Api; +use BaserCore\Test\Factory\ContentFactory; use BaserCore\Test\Factory\PermissionFactory; use BaserCore\Test\Scenario\InitAppScenario; use BaserCore\TestSuite\BcTestCase; @@ -91,7 +92,8 @@ public function test_view() { // データを生成 $this->loadFixtureScenario(BlogContentScenario::class, 1, 1, null, 'test', '/test'); - BlogPostFactory::make(['id' => '1', 'blog_content_id' => '1', 'title' => 'blog post', 'status' => true])->persist(); + BlogPostFactory::make(['id' => '1', 'blog_content_id' => '1', 'title' => 'blog post', 'eye_catch' => 'eye_catch.img', 'status' => true])->persist(); + ContentFactory::make(['type' => 'BlogContent', 'entity_id' => 1])->persist(); PermissionFactory::make()->allowGuest('/baser/api/*')->persist(); // APIを呼ぶ @@ -101,6 +103,7 @@ public function test_view() // 戻り値を確認 $result = json_decode((string)$this->_response->getBody()); $this->assertEquals(1, $result->blogPost->id); + $this->assertTextContains('/files/blog/1/blog_posts/eye_catch.img', $result->blogPost->_eyecatch); $this->assertEquals(1, $result->blogPost->blog_content_id); //存在しないBlogPostIDをテスト場合、 diff --git a/plugins/bc-blog/tests/TestCase/Model/Entity/BlogPostTest.php b/plugins/bc-blog/tests/TestCase/Model/Entity/BlogPostTest.php new file mode 100644 index 0000000000..d01dec5fc3 --- /dev/null +++ b/plugins/bc-blog/tests/TestCase/Model/Entity/BlogPostTest.php @@ -0,0 +1,74 @@ + + * Copyright (c) NPO baser foundation + * + * @copyright Copyright (c) NPO baser foundation + * @link https://basercms.net baserCMS Project + * @since 5.0.0 + * @license https://basercms.net/license/index.html MIT License + */ + +namespace BcBlog\Test\TestCase\Model\Entity; + +use BaserCore\TestSuite\BcTestCase; +use BcBlog\Model\Entity\BlogPost; +use BcBlog\Test\Scenario\BlogContentScenario; +use CakephpFixtureFactories\Scenario\ScenarioAwareTrait; + +/** + * Class BlogPostTest + */ +class BlogPostTest extends BcTestCase +{ + /** + * Trait + */ + use ScenarioAwareTrait; + + /** + * @var BlogPost + */ + public $BlogPost; + + /** + * Set Up + * + * @return void + */ + public function setUp(): void + { + parent::setUp(); + $this->BlogPost = $this->getTableLocator()->get('BlogPost.BlogPosts'); + } + + /** + * Tear Down + * + * @return void + */ + public function tearDown(): void + { + unset($this->BlogPost); + parent::tearDown(); + } + + /** + * test _get_eyecatch + */ + public function test_get_eyecatch() + { + $this->loadFixtureScenario(BlogContentScenario::class, 1, 1, null, 'test', '/test'); + $blogPost = new BlogPost([ + 'id' => 1, + 'blog_content_id' => 1, + 'no' => 1, + 'title' => 'blog post title', + 'status' => true, + 'eye_catch' => 'test.png' + ]); + $_eyecatch = $this->execPrivateMethod($blogPost, '_get_eyecatch', []); + $this->assertTextContains('/files/blog/1/blog_posts/test.png', $_eyecatch); + } + +}