1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace Tests\Libraries;
7
8use App\Libraries\BBCodeForDB;
9use App\Models\User;
10use Tests\TestCase;
11
12class BBCodeForDBTest extends TestCase
13{
14 /**
15 * @dataProvider examples
16 */
17 public function testGenerate($name, $path)
18 {
19 $baseFilePath = "{$path}/{$name}.base.txt";
20 $dbFilePath = "{$path}/{$name}.db.txt";
21
22 $output = (new BBCodeForDB(file_get_contents($baseFilePath)))->generate();
23
24 $this->assertStringEqualsFile($dbFilePath, $output);
25 }
26
27 public function testNewline()
28 {
29 $source = "[code]line one\r\nline two\rline three\nline four\r\nline five[/code]";
30 $expectedOutput = '[code:1]line one line two line three line four line five[/code:1]';
31
32 $output = (new BBCodeForDB($source))->generate();
33
34 $this->assertSame($expectedOutput, $output);
35 }
36
37 public function testProfile()
38 {
39 $user = User::factory()->create();
40 $emptyBbcode = new BBCodeForDB();
41 $name = $emptyBbcode->extraEscapes($user->username);
42
43 $source = "[profile]{$user->username}[/profile]";
44 $expectedOutput = "[profile={$user->getKey()}:1]{$name}[/profile:1]";
45
46 $output = (new BBCodeForDB($source))->generate();
47
48 $this->assertSame($expectedOutput, $output);
49 }
50
51 public function testProfileMismatchId()
52 {
53 $user = User::factory()->create();
54 $emptyBbcode = new BBCodeForDB();
55 $name = $emptyBbcode->extraEscapes($user->username);
56
57 $source = "[profile={$user->getKey()}]x[/profile]";
58 $expectedOutput = "[profile={$user->getKey()}:1]{$name}[/profile:1]";
59
60 $output = (new BBCodeForDB($source))->generate();
61
62 $this->assertSame($expectedOutput, $output);
63 }
64
65 public function testProfileInvalidUser()
66 {
67 $source = '[profile]a[/profile]'; // name is too short to match any users
68 $expectedOutput = '[profile:1]a[/profile:1]';
69
70 $output = (new BBCodeForDB($source))->generate();
71
72 $this->assertSame($expectedOutput, $output);
73 }
74
75 public static function examples()
76 {
77 return static::fileList(__DIR__.'/bbcode_examples', '.base.txt');
78 }
79
80 protected function setUp(): void
81 {
82 parent::setUp();
83
84 config_set('osu.bbcode.uid', '1');
85 }
86}