jonnybarnes.uk/tests/Unit/LikesTest.php
Jonny Barnes 5481956d78
Use uri for the like and bookmark permalinks
`uri` is the attribute every other postable model uses for its absolute
permalink on this site. Add it to Like, which had none, and rename
Bookmark's `local_uri` to match. `url` stays as the thing being liked or
bookmarked.

Refs #148

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 15:28:30 +00:00

78 lines
2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit;
use App\Models\Like;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class LikesTest extends TestCase
{
use RefreshDatabase;
#[Test]
public function like_uri_attribute_returns_our_permalink(): void
{
$like = Like::factory()->create();
$this->assertEquals(config('app.url').'/likes/'.$like->id, $like->uri);
}
#[Test]
public function we_can_set_the_author_url(): void
{
$like = new Like;
$like->author_url = 'https://joe.bloggs/';
$this->assertEquals('https://joe.bloggs', $like->author_url);
}
#[Test]
public function we_do_not_modify_plain_text_content(): void
{
$like = new Like;
$like->url = 'https://example.org/post/123';
$like->content = 'some plaintext content';
$like->save();
$this->assertEquals('some plaintext content', $like->content);
}
#[Test]
public function we_can_handle_blank_content(): void
{
$like = new Like;
$like->url = 'https://example.org/post/123';
$like->content = null;
$like->save();
$this->assertNull($like->content);
}
#[Test]
public function html_like_content_is_filtered(): void
{
$htmlEvil = <<<'HTML'
<div class="h-entry">
<div class="e-content">
<p>Hello</p>
<img src="javascript:evil();" onload="evil();" />
</div>
</div>
HTML;
$htmlFiltered = <<<'HTML'
<p>Hello</p>
<img />
HTML;
$like = new Like;
$like->url = 'https://example.org/post/123';
$like->content = $htmlEvil;
$like->save();
// HTMLPurifier will leave the whitespace before the <img> tag
// trim it, saving whitespace in $htmlFiltered can get removed by text editors
$this->assertEquals($htmlFiltered, trim($like->content));
}
}