Skip to content

Commit

Permalink
Throw invalid name on starting or ending with "-"
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Westergaard committed Jul 18, 2023
1 parent eed5a5f commit 9bfa6c7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Helper/EventHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function toArray(): array
throw Ga4EventException::throwNameMissing();
} elseif (strlen($name) > 40) {
throw Ga4EventException::throwNameTooLong();
} elseif (preg_match('/[^\w\d\-]/', $name)) {
} elseif (preg_match('/[^\w\d\-]|^\-|\-$/', $name)) {
throw Ga4EventException::throwNameInvalid();
} elseif (in_array($name, EventType::RESERVED_NAMES) && !($this instanceof GtmEventType)) {
throw Ga4EventException::throwNameReserved($name);
Expand Down
36 changes: 36 additions & 0 deletions test/Unit/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,42 @@ public function getName(): string
$class->toArray();
}

public function test_throw_name_invalid_starting_line()
{
$mock = new class extends Event\Refund
{
public function getName(): string
{
return '-almost-valid-name';
}
};

$class = $mock::new()->setTransactionId(1);

$this->expectException(Ga4EventException::class);
$this->expectExceptionCode(Ga4Exception::EVENT_NAME_INVALID);

$class->toArray();
}

public function test_throw_name_invalid_ending_line()
{
$mock = new class extends Event\Refund
{
public function getName(): string
{
return 'almost-valid-name-';
}
};

$class = $mock::new()->setTransactionId(1);

$this->expectException(Ga4EventException::class);
$this->expectExceptionCode(Ga4Exception::EVENT_NAME_INVALID);

$class->toArray();
}

public function test_throw_name_reserved()
{
$mock = new class extends Event\Refund
Expand Down

0 comments on commit 9bfa6c7

Please sign in to comment.