assertStringContainsString('/go/', $go->goUrl('https://example.com/path')); $this->assertSame('http://laralog.test/posts/1', $go->goUrl('http://laralog.test/posts/1')); $this->assertSame('/posts/1', $go->goUrl('/posts/1')); $this->assertSame('mailto:a@b.com', $go->goUrl('mailto:a@b.com')); $this->assertSame('#anchor', $go->goUrl('#anchor')); } public function test_go_redirect_counts_clicks(): void { $redirect = Redirect::create([ 'hash' => 'abc123def0', 'target_url' => 'https://example.com/target', ]); $this->get('/go/abc123def0') ->assertRedirect('https://example.com/target'); $redirect->refresh(); $this->assertSame(1, $redirect->clicks); $this->assertNotNull($redirect->last_clicked_at); } public function test_disabled_redirect_returns_410(): void { Redirect::create([ 'hash' => 'blocked0000', 'target_url' => 'https://example.com/bad', 'status' => 'disabled', ]); $this->get('/go/blocked0000')->assertStatus(410); } public function test_same_url_reuses_same_hash(): void { $a = app(UrlRedirector::class)->goUrl('https://example.com/dup'); $b = app(UrlRedirector::class)->goUrl('https://example.com/dup'); $this->assertSame($a, $b); $this->assertSame(1, Redirect::query()->where('target_url', 'https://example.com/dup')->count()); } public function test_post_render_rewrites_external_links(): void { $post = Post::create([ 'title' => '外链测试', 'slug' => 'go-test', 'content' => "正文 外链站内", 'content_format' => 'html', 'status' => 'published', 'published_at' => now(), ]); $html = app(PostContentRenderer::class)->render($post); $this->assertStringContainsString('href="http://laralog.test/go/', $html); $this->assertStringContainsString('href="http://laralog.test/posts/1"', $html); $this->assertStringNotContainsString('href="https://example.com/x"', $html); } public function test_comment_body_autolinks_and_redirects(): void { $html = comment_body('看看 https://example.com/note 这个链接'); $this->assertStringContainsString('href="http://laralog.test/go/', $html); $this->assertStringContainsString('rel="nofollow noopener"', $html); $this->assertStringNotContainsString('href="https://example.com/note"', $html); } }