summaryrefslogtreecommitdiff
path: root/tests/units/Helper/TextHelperTest.php
blob: 9d862900fbc80f67da8c62a60642d5a6068d4655 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php

require_once __DIR__.'/../Base.php';

use Kanboard\Helper\TextHelper;
use Kanboard\Model\ProjectModel;
use Kanboard\Model\TaskCreationModel;
use Kanboard\Model\UserModel;

class TextHelperTest extends Base
{
    public function testImplode()
    {
        $textHelper = new TextHelper($this->container);
        $html = '&lt;img src=x onerror=alert(0)&gt;';
        $this->assertEquals($html, $textHelper->implode(', ', array('<img src=x onerror=alert(0)>')));
    }

    public function testMarkdownTaskLink()
    {
        $textHelper = new TextHelper($this->container);
        $projectModel = new ProjectModel($this->container);
        $taskCreationModel = new TaskCreationModel($this->container);

        $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1')));
        $this->assertTrue($projectModel->enablePublicAccess(1));
        $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1)));
        $project = $projectModel->getById(1);

        $this->assertEquals('<p>Test</p>', $textHelper->markdown('Test'));

        $this->assertEquals(
            '<p>Task <a href="?controller=TaskViewController&amp;action=show&amp;task_id=123">#123</a></p>',
            $textHelper->markdown('Task #123')
        );

        $this->assertEquals(
            '<p>Task #123</p>',
            $textHelper->markdown('Task #123', true)
        );

        $this->assertEquals(
            '<p>Task <a href="http://localhost/?controller=TaskViewController&amp;action=readonly&amp;token='.$project['token'].'&amp;task_id=1">#1</a></p>',
            $textHelper->markdown('Task #1', true)
        );

        $this->assertEquals(
            '<p>Check that: <a href="http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454">http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454</a></p>',
            $textHelper->markdown(
                'Check that: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454'
            )
        );

        $this->assertEquals(
            '<p><a href="http://localhost">item #123 is here</a></p>',
            $textHelper->markdown(
                '[item #123 is here](http://localhost)'
            )
        );
    }

    public function testMarkdownUserLink()
    {
        $textHelper = new TextHelper($this->container);
        $userModel = new UserModel($this->container);

        $this->assertEquals(2, $userModel->create(array('username' => 'firstname.lastname', 'name' => 'Firstname Lastname')));

        $this->assertEquals(
            '<p>Text <a href="?controller=UserViewController&amp;action=profile&amp;user_id=1" class="user-mention-link" title="admin">@admin</a> @notfound</p>',
            $textHelper->markdown('Text @admin @notfound')
        );

        $this->assertEquals(
            '<p>Text <a href="?controller=UserViewController&amp;action=profile&amp;user_id=1" class="user-mention-link" title="admin">@admin</a>,</p>',
            $textHelper->markdown('Text @admin,')
        );

        $this->assertEquals(
            '<p>Text <a href="?controller=UserViewController&amp;action=profile&amp;user_id=1" class="user-mention-link" title="admin">@admin</a>!</p>',
            $textHelper->markdown('Text @admin!')
        );

        $this->assertEquals(
            '<p>Text <a href="?controller=UserViewController&amp;action=profile&amp;user_id=1" class="user-mention-link" title="admin">@admin</a>? </p>',
            $textHelper->markdown('Text @admin? ')
        );

        $this->assertEquals(
            '<p>Text <a href="?controller=UserViewController&amp;action=profile&amp;user_id=1" class="user-mention-link" title="admin">@admin</a>.</p>',
            $textHelper->markdown('Text @admin.')
        );

        $this->assertEquals(
            '<p>Text <a href="?controller=UserViewController&amp;action=profile&amp;user_id=1" class="user-mention-link" title="admin">@admin</a>: test</p>',
            $textHelper->markdown('Text @admin: test')
        );

        $this->assertEquals(
            '<p>Text <a href="?controller=UserViewController&amp;action=profile&amp;user_id=1" class="user-mention-link" title="admin">@admin</a>: test</p>',
            $textHelper->markdown('Text @admin: test')
        );

        $this->assertEquals(
            '<p>Text <a href="?controller=UserViewController&amp;action=profile&amp;user_id=2" class="user-mention-link" title="Firstname Lastname">@firstname.lastname</a>. test</p>',
            $textHelper->markdown('Text @firstname.lastname. test')
        );

        $this->assertEquals('<p>Text @admin @notfound</p>', $textHelper->markdown('Text @admin @notfound', true));

        $this->assertEquals(
            '<p><a href="http://localhost">mention @admin at localhost</a></p>',
            $textHelper->markdown(
                '[mention @admin at localhost](http://localhost)'
            )
        );
    }

    public function testFormatBytes()
    {
        $textHelper = new TextHelper($this->container);

        $this->assertEquals('0', $textHelper->bytes(0));
        $this->assertEquals('1k', $textHelper->bytes(1024));
        $this->assertEquals('33.71k', $textHelper->bytes(34520));
    }

    public function testContains()
    {
        $textHelper = new TextHelper($this->container);

        $this->assertTrue($textHelper->contains('abc', 'b'));
        $this->assertFalse($textHelper->contains('abc', 'd'));
    }

    public function testInList()
    {
        $textHelper = new TextHelper($this->container);
        $this->assertEquals('?', $textHelper->in('a', array('b' => 'c')));
        $this->assertEquals('c', $textHelper->in('b', array('b' => 'c')));
    }
}