Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions plugins/bc-blog/src/Model/Table/BlogPostsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\I18n\FrozenTime;
use Cake\ORM\Exception\PersistenceFailedException;
use Cake\ORM\Query;
use Cake\Utility\Hash;
use Cake\Validation\Validator;
Expand Down Expand Up @@ -675,8 +674,17 @@ public function copy($id = null, BlogPost $data = null)
}

$data->user_id = BcUtil::loginUser()['id'];
if ($data->name) $data->name .= '_copy';
$data->title .= '_copy';
if ($data->name) {
$baseName = $data->name;
$copySuffix = '_copy';
$data->name = mb_substr($baseName, 0, 255 - mb_strlen($copySuffix)) . $copySuffix;
for ($i = 1; $i <= 10 && $this->exists(['BlogPosts.name' => $data->name]); $i++) {
$suffix = '_' . $i;
$duplicateSuffix = $copySuffix . $suffix;
$data->name = mb_substr($baseName, 0, 255 - mb_strlen($duplicateSuffix)) . $duplicateSuffix;
}
}
$data->title = mb_substr($data->title, 0, 250) . '_copy';
Comment thread
kaburk marked this conversation as resolved.
$data->no = $this->getMax('no', ['BlogPosts.blog_content_id' => $data->blog_content_id]) + 1;
$data->status = false;
$data->posted = \Cake\I18n\DateTime::now();
Expand Down Expand Up @@ -714,13 +722,6 @@ public function copy($id = null, BlogPost $data = null)
'oldData' => $oldData,
]);
return $result;
} catch (PersistenceFailedException $e) {
if ($e->getEntity()->getError('name')) {
$data->eye_catch = $eyeCatch;
return $this->copy(null, $data);
} else {
return false;
}
} catch (BcException $e) {
throw $e;
}
Expand Down
60 changes: 60 additions & 0 deletions plugins/bc-blog/tests/TestCase/Model/BlogPostsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,66 @@ public function testCopy()
$this->assertFalse($result->status);
}

/**
* コピーする(255文字境界で _copy を維持できる)
*/
public function testCopyWithMaxLengthName()
{
$this->loadFixtureScenario(InitAppScenario::class);
$this->loginAdmin($this->getRequest());

$sourceName = str_repeat('a', 255);
$sourceTitle = str_repeat('b', 255);
BlogPostFactory::make([
'id' => 100,
'blog_content_id' => 6,
'no' => 100,
'name' => $sourceName,
'title' => $sourceTitle,
'status' => 1,
])->persist();

$result = $this->BlogPostsTable->copy(100);

$this->assertEquals(255, mb_strlen($result->name));
$this->assertStringEndsWith('_copy', $result->name);
$this->assertEquals(255, mb_strlen($result->title));
$this->assertStringEndsWith('_copy', $result->title);
}

/**
* コピーする(重複時に _copy_1 を維持できる)
*/
public function testCopyWithDuplicatedMaxLengthName()
{
$this->loadFixtureScenario(InitAppScenario::class);
$this->loginAdmin($this->getRequest());

$sourceName = str_repeat('a', 255);
$copiedName = mb_substr($sourceName, 0, 255 - mb_strlen('_copy')) . '_copy';
BlogPostFactory::make([
'id' => 101,
'blog_content_id' => 6,
'no' => 101,
'name' => $sourceName,
'title' => '元記事',
'status' => 1,
])->persist();
BlogPostFactory::make([
'id' => 102,
'blog_content_id' => 6,
'no' => 102,
'name' => $copiedName,
'title' => '重複記事',
'status' => 1,
])->persist();

$result = $this->BlogPostsTable->copy(101);

$this->assertEquals(255, mb_strlen($result->name));
$this->assertStringEndsWith('_copy_1', $result->name);
}

/**
* プレビュー用のデータを生成する
*/
Expand Down
Loading