From de3f6ec102e6e97a821c1eb7d44d37014cc0a748 Mon Sep 17 00:00:00 2001 From: sakaguchi Date: Wed, 2 Dec 2026 11:45:22 +0900 Subject: [PATCH] =?UTF-8?q?fix=20#4699=20=E3=80=90=E3=82=B7=E3=82=B9?= =?UTF-8?q?=E3=83=86=E3=83=A0=E3=80=91=E3=82=A2=E3=83=83=E3=83=97=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=97=E3=81=9F=E7=94=BB=E5=83=8F=E3=81=AE?= =?UTF-8?q?=20URL=20=E3=81=AB=E8=A1=A8=E7=A4=BA=E3=81=AE=E3=81=9F=E3=81=B3?= =?UTF-8?q?=E3=81=AB=E4=B9=B1=E6=95=B0=E3=81=8C=E4=BB=98=E3=81=8D=E3=80=81?= =?UTF-8?q?=E3=83=96=E3=83=A9=E3=82=A6=E3=82=B6=E3=81=AE=E3=82=AD=E3=83=A3?= =?UTF-8?q?=E3=83=83=E3=82=B7=E3=83=A5=E3=81=8C=E5=8A=B9=E3=81=8B=E3=81=AA?= =?UTF-8?q?=E3=81=84=E5=95=8F=E9=A1=8C=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/View/Helper/BcUploadHelper.php | 16 +++++-- .../View/Helper/BcUploadHelperTest.php | 45 +++++++++++++++++-- .../TestCase/View/Helper/BlogHelperTest.php | 3 +- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/plugins/baser-core/src/View/Helper/BcUploadHelper.php b/plugins/baser-core/src/View/Helper/BcUploadHelper.php index 94177db289..caf7770e57 100755 --- a/plugins/baser-core/src/View/Helper/BcUploadHelper.php +++ b/plugins/baser-core/src/View/Helper/BcUploadHelper.php @@ -17,6 +17,7 @@ use Cake\Utility\Inflector; use Cake\View\Helper; use Cake\ORM\TableRegistry; +use Cake\Routing\Asset; use BaserCore\Utility\BcUtil; use BaserCore\Annotation\NoTodo; use BaserCore\Error\BcException; @@ -453,10 +454,10 @@ public function uploadImage($fieldName, $entity, $options = []) if ($fileExists || $options['force']) { if ($check && !$mostSizeExists) { - $mostSizeUrl = $fileUrl . $subdir . $imgPrefix . $basename . $imgSuffix . '.' . $ext . '?' . rand(); + $mostSizeUrl = $fileUrl . $subdir . $imgPrefix . $basename . $imgSuffix . '.' . $ext; $mostSizeExists = true; } elseif (!$mostSizeExists && !$maxSizeExists) { - $maxSizeUrl = $fileUrl . $subdir . $imgPrefix . $basename . $imgSuffix . '.' . $ext . '?' . rand(); + $maxSizeUrl = $fileUrl . $subdir . $imgPrefix . $basename . $imgSuffix . '.' . $ext; $maxSizeExists = true; } } @@ -464,13 +465,20 @@ public function uploadImage($fieldName, $entity, $options = []) } if (!isset($mostSizeUrl)) { - $mostSizeUrl = $fileUrl . $fileName . '?' . rand(); + $mostSizeUrl = $fileUrl . $fileName; } if (!isset($maxSizeUrl)) { - $maxSizeUrl = $fileUrl . $fileName . '?' . rand(); + $maxSizeUrl = $fileUrl . $fileName; } } + if ($fileName != $options['noimage'] && !$options['tmp']) { + // 画像を差し替えたときだけ URL が変わるよう、乱数ではなくファイルの更新日時を付ける(CakePHP の Asset.timestamp と同じ仕組み) + // サイトの Asset.timestamp の設定に関係なく付ける(差し替えた画像が古いまま表示されないように) + $mostSizeUrl = Asset::assetTimestamp($mostSizeUrl, 'force'); + $maxSizeUrl = Asset::assetTimestamp($maxSizeUrl, 'force'); + } + $output = $options['output']; $link = $options['link']; $noimage = $options['noimage']; diff --git a/plugins/baser-core/tests/TestCase/View/Helper/BcUploadHelperTest.php b/plugins/baser-core/tests/TestCase/View/Helper/BcUploadHelperTest.php index 6b5ddf9d78..06a6e89957 100755 --- a/plugins/baser-core/tests/TestCase/View/Helper/BcUploadHelperTest.php +++ b/plugins/baser-core/tests/TestCase/View/Helper/BcUploadHelperTest.php @@ -113,7 +113,7 @@ public function testUploadImage() { // オプションなし $result = $this->BcUpload->uploadImage('image', new Content(['image' => 'template1.jpg'])); - $this->assertMatchesRegularExpression('/^]+?\"[^>]+?>]+?\"[^>]+?><\/a>/', $result); + $this->assertMatchesRegularExpression('/^]+?>]+?><\/a>/', $result); // サイズ指定あり $options = [ @@ -121,7 +121,7 @@ public function testUploadImage() 'height' => '80', ]; $result = $this->BcUpload->uploadImage('image', new Content(['image' => 'template1.jpg']), $options); - $this->assertMatchesRegularExpression('/^]+?\"[^>]+?>]+?\"[^>]+?alt="" width="100" height="80"[^>]*?><\/a>/', $result); + $this->assertMatchesRegularExpression('/^]+?>]+?alt="" width="100" height="80"[^>]*?><\/a>/', $result); // 一時ファイルへのリンク(デフォルトでは colorbox 付きリンクで拡大できる) $options = [ @@ -146,7 +146,46 @@ public function testUploadImage() 'output' => 'url' ]; $result = $this->BcUpload->uploadImage('image', new Content(['image' => 'template1.jpg']), $options); - $this->assertMatchesRegularExpression('/^\/files\/contents\/template1\.jpg\?[0-9]+/', $result); + // ファイルが無いので、更新日時は付かない(付くことは testUploadImage_timestamp で確かめる) + $this->assertEquals('/files/contents/template1.jpg', $result); + } + + /** + * test uploadImage - 画像の URL には、乱数ではなくファイルの更新日時が付くこと + * 画像を差し替えたときだけ URL が変わり、ふだんはブラウザや CDN のキャッシュが効く + */ + public function testUploadImage_timestamp() + { + $dir = WWW_ROOT . 'files' . DS . 'contents' . DS; + $createdDir = !is_dir($dir); + if ($createdDir) mkdir($dir, 0777, true); + $path = $dir . 'timestamp_test.jpg'; + file_put_contents($path, 'test'); + touch($path, 1700000000); + clearstatcache(); + $entity = new Content(['image' => 'timestamp_test.jpg']); + + // URL に更新日時が付く + $result = $this->BcUpload->uploadImage('image', $entity, ['output' => 'url']); + $this->assertEquals('/files/contents/timestamp_test.jpg?1700000000', $result); + // 何度出しても同じ URL + $this->assertEquals($result, $this->BcUpload->uploadImage('image', $entity, ['output' => 'url'])); + + // 差し替える(更新日時が変わる)と URL が変わる + touch($path, 1700000100); + clearstatcache(); + $this->assertEquals('/files/contents/timestamp_test.jpg?1700000100', $this->BcUpload->uploadImage('image', $entity, ['output' => 'url'])); + + // 画像タグとリンクの両方に付く + $result = $this->BcUpload->uploadImage('image', $entity); + $this->assertStringContainsString('href="/files/contents/timestamp_test.jpg?1700000100"', $result); + $this->assertStringContainsString('src="/files/contents/timestamp_test.jpg?1700000100"', $result); + + unlink($path); + if ($createdDir) rmdir($dir); + + // ファイルが無いときは付かない + $this->assertEquals('/files/contents/timestamp_test.jpg', $this->BcUpload->uploadImage('image', $entity, ['output' => 'url'])); } /** diff --git a/plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php b/plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php index 484312c16e..acf8256296 100755 --- a/plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php +++ b/plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php @@ -985,7 +985,8 @@ public function testGetEyeCatch() // outputはurlのテスト $options = ['table' => 'BcBlog.BlogPosts', 'output' => 'url']; $result = $this->Blog->getEyeCatch($post, $options); - $expected = '/files/blog/1/blog_posts/test-eye_catch.jpg?'; + // 画像ファイルがあれば更新日時(?数字)が付く。テストの環境にはファイルが無いので、URL の始まりだけを見る + $expected = '/files/blog/1/blog_posts/test-eye_catch.jpg'; $this->assertEquals($expected, substr($result, 0, strlen($expected))); // outputはtagのテスト