Skip to content

Commit

Permalink
[tag_edit] abort on error rather than only applying some tags
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Jan 11, 2024
1 parent acd3abc commit 4b8bc82
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
19 changes: 8 additions & 11 deletions core/imageboard/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,24 +524,21 @@ public function set_tags(array $unfiltered_tags): void
{
global $cache, $database, $page;

$unfiltered_tags = array_unique($unfiltered_tags);
$tags = array_unique($unfiltered_tags);

$tags = [];
foreach ($unfiltered_tags as $tag) {
foreach ($tags as $tag) {
if (mb_strlen($tag, 'UTF-8') > 255) {
$page->flash("Can't set a tag longer than 255 characters");
continue;
throw new TagSetException("Can't set a tag longer than 255 characters");
}
if (str_starts_with($tag, "-")) {
$page->flash("Can't set a tag which starts with a minus");
continue;
throw new TagSetException("Can't set a tag which starts with a minus");
}
if (str_contains($tag, "*")) {
throw new TagSetException("Can't set a tag which contains a wildcard (*)");
}

$tags[] = $tag;
}

if (count($tags) <= 0) {
throw new SCoreException('Tried to set zero tags');
throw new TagSetException('Tried to set zero tags');
}

if (strtolower(Tag::implode($tags)) != strtolower($this->get_tag_list())) {
Expand Down
13 changes: 8 additions & 5 deletions ext/tag_edit/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ public function testInvalidChange()
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx");
$image = Image::by_id($image_id);

try {
$e = $this->assertException(TagSetException::class, function () use ($image) {
send_event(new TagSetEvent($image, []));
$this->fail();
} catch (SCoreException $e) {
$this->assertEquals("Tried to set zero tags", $e->error);
}
});
$this->assertEquals("Tried to set zero tags", $e->getMessage());

$e = $this->assertException(TagSetException::class, function () use ($image) {
send_event(new TagSetEvent($image, ["*test*"]));
});
$this->assertEquals("Can't set a tag which contains a wildcard (*)", $e->getMessage());
}

public function testTagEdit_tooLong()
Expand Down

0 comments on commit 4b8bc82

Please sign in to comment.