diff --git a/app/Io/CheckinCsvImporter.php b/app/Io/CheckinCsvImporter.php index f91a063..364a007 100644 --- a/app/Io/CheckinCsvImporter.php +++ b/app/Io/CheckinCsvImporter.php @@ -79,7 +79,11 @@ class CheckinCsvImporter break; } if (mb_strlen($tag) > 255) { - $errors[] = "{$line} 行 : {$column}列は255文字以内にしてください。"; + $errors[] = "{$line} 行 : {$column}は255文字以内にしてください。"; + continue 2; + } + if (strpos($tag, "\n") !== false) { + $errors[] = "{$line} 行 : {$column}に改行を含めることはできません。"; continue 2; } @@ -87,9 +91,8 @@ class CheckinCsvImporter $tagIds[] = $tag->id; } } - $ejaculation->tags()->sync($tagIds); - $ejaculation->save(); + $ejaculation->tags()->sync($tagIds); } if (!empty($errors)) { diff --git a/tests/Unit/Io/CheckinCsvImporterTest.php b/tests/Unit/Io/CheckinCsvImporterTest.php index cf91770..f3ecfb3 100644 --- a/tests/Unit/Io/CheckinCsvImporterTest.php +++ b/tests/Unit/Io/CheckinCsvImporterTest.php @@ -168,4 +168,67 @@ class CheckinCsvImporterTest extends TestCase $importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/link-not-url.utf8.csv'); $importer->execute(); } + + public function testTag1UTF8() + { + $user = factory(User::class)->create(); + + $importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/tag1.utf8.csv'); + $importer->execute(); + $ejaculation = $user->ejaculations()->first(); + $tags = $ejaculation->tags()->get(); + + $this->assertSame(1, $user->ejaculations()->count()); + $this->assertCount(1, $tags); + $this->assertEquals('貧乳', $tags[0]->name); + } + + public function testTag2UTF8() + { + $user = factory(User::class)->create(); + + $importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/tag2.utf8.csv'); + $importer->execute(); + $ejaculation = $user->ejaculations()->first(); + $tags = $ejaculation->tags()->get(); + + $this->assertSame(1, $user->ejaculations()->count()); + $this->assertCount(2, $tags); + $this->assertEquals('貧乳', $tags[0]->name); + $this->assertEquals('巨乳', $tags[1]->name); + } + + public function testTagOverLengthUTF8() + { + $user = factory(User::class)->create(); + $this->expectException(CsvImportException::class); + $this->expectExceptionMessage('3 行 : タグ1は255文字以内にしてください。'); + + $importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/tag-over-length.utf8.csv'); + $importer->execute(); + } + + public function testTagCantAcceptJumpedColumnUTF8() + { + $user = factory(User::class)->create(); + + $importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/tag-jumped-column.utf8.csv'); + $importer->execute(); + $ejaculation = $user->ejaculations()->first(); + $tags = $ejaculation->tags()->get(); + + $this->assertSame(1, $user->ejaculations()->count()); + $this->assertCount(1, $tags); + $this->assertEquals('貧乳', $tags[0]->name); + } + + public function testTagCantAcceptMultilineUTF8() + { + $user = factory(User::class)->create(); + $this->expectException(CsvImportException::class); + $this->expectExceptionMessage('2 行 : タグ1に改行を含めることはできません。'); + + $importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/tag-multiline.utf8.csv'); + $importer->execute(); + } } diff --git a/tests/fixture/Csv/.editorconfig b/tests/fixture/Csv/.editorconfig new file mode 100644 index 0000000..c9e4d60 --- /dev/null +++ b/tests/fixture/Csv/.editorconfig @@ -0,0 +1,2 @@ +[*.csv] +end_of_line = crlf diff --git a/tests/fixture/Csv/tag-jumped-column.utf8.csv b/tests/fixture/Csv/tag-jumped-column.utf8.csv new file mode 100644 index 0000000..773db36 --- /dev/null +++ b/tests/fixture/Csv/tag-jumped-column.utf8.csv @@ -0,0 +1,2 @@ +日時,タグ1,タグ3 +2020/01/23 06:01,貧乳,巨乳 diff --git a/tests/fixture/Csv/tag-multiline.utf8.csv b/tests/fixture/Csv/tag-multiline.utf8.csv new file mode 100644 index 0000000..ee1bd38 --- /dev/null +++ b/tests/fixture/Csv/tag-multiline.utf8.csv @@ -0,0 +1,3 @@ +日時,タグ1 +2020/01/23 06:01,"複数行の +タグ" diff --git a/tests/fixture/Csv/tag-over-length.utf8.csv b/tests/fixture/Csv/tag-over-length.utf8.csv new file mode 100644 index 0000000..891308e --- /dev/null +++ b/tests/fixture/Csv/tag-over-length.utf8.csv @@ -0,0 +1,3 @@ +日時,タグ1 +2020/01/23 06:01,ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo +2020/01/23 06:02,ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooox diff --git a/tests/fixture/Csv/tag1.utf8.csv b/tests/fixture/Csv/tag1.utf8.csv new file mode 100644 index 0000000..1d7561c --- /dev/null +++ b/tests/fixture/Csv/tag1.utf8.csv @@ -0,0 +1,2 @@ +日時,タグ1 +2020/01/23 06:01,貧乳 diff --git a/tests/fixture/Csv/tag2.utf8.csv b/tests/fixture/Csv/tag2.utf8.csv new file mode 100644 index 0000000..10e82fd --- /dev/null +++ b/tests/fixture/Csv/tag2.utf8.csv @@ -0,0 +1,2 @@ +日時,タグ1,タグ2 +2020/01/23 06:01,貧乳,巨乳