Skip to content

Commit

Permalink
Add all available fields to message
Browse files Browse the repository at this point in the history
  • Loading branch information
curry684 committed Apr 21, 2023
1 parent 026a5a4 commit 6f54c43
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Akismet.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function setTesting(bool $isTesting): static
}

/**
* @param array<string, string> $parameters
* @param array<string, string|string[]> $parameters
*/
private function call(string $method, array $parameters = []): ResponseInterface
{
Expand Down
129 changes: 126 additions & 3 deletions src/AkismetMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@

class AkismetMessage
{
/** @var array<string, string> */
private array $values = [];
/** @var array{comment_context: string[]}&string[] */
private array $values; // @phpstan-ignore-line

public function __construct()
{
$this->values = [
'comment_context' => [],
];
}

public function getAuthor(): ?string
{
Expand All @@ -40,6 +47,16 @@ public function setAuthorEmail(?string $email): static
return $this->set('comment_author_email', $email);
}

public function getAuthorUrl(): ?string
{
return $this->get('comment_author_url');
}

public function setAuthorUrl(?string $url): static
{
return $this->set('comment_author_url', $url);
}

public function getContent(): ?string
{
return $this->get('comment_content');
Expand All @@ -50,6 +67,90 @@ public function setContent(?string $content): static
return $this->set('comment_content', $content);
}

public function getDateCreated(): ?\DateTimeImmutable
{
$date = $this->get('comment_date_gmt');

return null !== $date ? new \DateTimeImmutable($date) : null;
}

public function setDateCreated(?\DateTimeInterface $date): static
{
return $this->set('comment_date_gmt', $date?->format('c'));
}

public function getDateModified(): ?\DateTimeImmutable
{
$date = $this->get('comment_post_modified_gmt');

return null !== $date ? new \DateTimeImmutable($date) : null;
}

public function setDateModified(?\DateTimeInterface $date): static
{
return $this->set('comment_post_modified_gmt', $date?->format('c'));
}

public function getEncoding(): ?string
{
return $this->get('blog_charset');
}

public function setEncoding(?string $encoding): static
{
return $this->set('blog_charset', $encoding);
}

public function clearHoneyPot(): static
{
return $this->set('honeypot_field_name', null)->set('hidden_honeypot_field', null);
}

public function getHoneyPotFieldName(): ?string
{
return $this->get('honeypot_field_name');
}

public function getHoneyPotValue(): ?string
{
return $this->get('hidden_honeypot_field');
}

public function setHoneyPot(string $fieldName, string $value): static
{
return $this->set('honeypot_field_name', $fieldName)->set('hidden_honeypot_field', $value);
}

public function getLanguages(): ?string
{
return $this->get('blog_lang');
}

public function setLanguages(?string $languages): static
{
return $this->set('blog_lang', $languages);
}

public function getPermalink(): ?string
{
return $this->get('permalink');
}

public function setPermalink(?string $permalink): static
{
return $this->set('permalink', $permalink);
}

public function getRecheckReason(): ?string
{
return $this->get('recheck_reason');
}

public function setRecheckReason(?string $reason): static
{
return $this->set('recheck_reason', $reason);
}

public function getReferrer(): ?string
{
return $this->get('referrer');
Expand Down Expand Up @@ -100,8 +201,30 @@ public function setUserRole(?string $role): static
return $this->set('user_role', $role);
}

public function addContext(string $context): static
{
$this->values['comment_context'][] = $context;

return $this;
}

public function clearContext(): static
{
$this->values['comment_context'] = [];

return $this;
}

/**
* @return string[]
*/
public function getContext(): array
{
return $this->values['comment_context'];
}

/**
* @return array<string, string>
* @return array<string, string|string[]>
*/
public function getValues(): array
{
Expand Down
35 changes: 35 additions & 0 deletions tests/AkismetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,56 @@ public function testMessageAccessors(): void
'HTTP_USER_AGENT' => 'Custom Browser 684',
]);

$created = new \DateTime('2023-02-03T12:34:56+02:00');
$modified = new \DateTimeImmutable('2023-03-04T23:45:01+05:00');

$message = AkismetMessage::fromRequest($request)
->setAuthor('foo')
->setAuthorEmail('[email protected]')
->setAuthorUrl('https://example.org')
->setContent('foo bar')
->setDateCreated($created)
->setDateModified($modified)
->setEncoding('UTF-8')
->setHoneyPot('hidden_field', 'Eric Jones')
->setLanguages('en, nl_nl')
->setPermalink('https://example.org/post/1')
->setRecheckReason('edit')
->setType(MessageType::BLOG_POST)
->setUserRole('guest')
->addContext('cooking')
->addContext('recipes')
->addContext('bbq')
;

$this->assertSame('foo', $message->getAuthor());
$this->assertSame('[email protected]', $message->getAuthorEmail());
$this->assertSame('https://example.org', $message->getAuthorUrl());
$this->assertSame('foo bar', $message->getContent());
$this->assertEquals($created, $message->getDateCreated());
$this->assertEquals($modified, $message->getDateModified());
$this->assertSame('UTF-8', $message->getEncoding());
$this->assertSame('hidden_field', $message->getHoneyPotFieldName());
$this->assertSame('Eric Jones', $message->getHoneyPotValue());
$this->assertSame('en, nl_nl', $message->getLanguages());
$this->assertSame('https://example.org/post/1', $message->getPermalink());
$this->assertSame('edit', $message->getRecheckReason());
$this->assertSame('https://www.google.com', $message->getReferrer());
$this->assertSame(MessageType::BLOG_POST, $message->getType());
$this->assertSame('Custom Browser 684', $message->getUserAgent());
$this->assertSame('guest', $message->getUserRole());
$this->assertSame('12.34.56.78', $message->getUserIP());

$this->assertCount(3, $message->getContext());
$this->assertSame('recipes', $message->getContext()[1]);

$message->clearHoneyPot();
$this->assertNull($message->getHoneyPotFieldName());
$this->assertNull($message->getHoneyPotValue());

$message->clearContext();
$this->assertCount(0, $message->getContext());

$message->setReferrer(null);
$this->assertNull($message->getReferrer());
}
Expand Down Expand Up @@ -129,6 +162,8 @@ public function testSpamCommentCheck(): void
->setAuthorEmail('[email protected]')
->setContent('You have won $700.000 in our raffle, visit https://spam.ru for information!!!')
->setType(MessageType::REPLY)
->addContext('bbq')
->addContext('recipes')
);

$this->assertTrue($response->isSpam());
Expand Down

0 comments on commit 6f54c43

Please sign in to comment.