summaryrefslogtreecommitdiff
path: root/tests/units/Core/ExternalLink/ExternalLinkManagerTest.php
blob: d284a80b3b31b809153e443ed9af878c2c6d3235 (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
<?php

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

use Kanboard\Core\ExternalLink\ExternalLinkManager;
use Kanboard\ExternalLink\WebLinkProvider;
use Kanboard\ExternalLink\AttachmentLinkProvider;

class ExternalLinkManagerTest extends Base
{
    public function testRegister()
    {
        $externalLinkManager = new ExternalLinkManager($this->container);
        $webLinkProvider = new WebLinkProvider($this->container);
        $attachmentLinkProvider = new AttachmentLinkProvider($this->container);

        $externalLinkManager->register($webLinkProvider);
        $externalLinkManager->register($attachmentLinkProvider);

        $this->assertInstanceOf(get_class($webLinkProvider), $externalLinkManager->getProvider($webLinkProvider->getType()));
        $this->assertInstanceOf(get_class($attachmentLinkProvider), $externalLinkManager->getProvider($attachmentLinkProvider->getType()));
    }

    public function testGetProviderNotFound()
    {
        $externalLinkManager = new ExternalLinkManager($this->container);

        $this->setExpectedException('\Kanboard\Core\ExternalLink\ExternalLinkProviderNotFound');
        $externalLinkManager->getProvider('not found');
    }

    public function testGetTypes()
    {
        $externalLinkManager = new ExternalLinkManager($this->container);
        $webLinkProvider = new WebLinkProvider($this->container);
        $attachmentLinkProvider = new AttachmentLinkProvider($this->container);

        $this->assertEquals(array(ExternalLinkManager::TYPE_AUTO => 'Auto'), $externalLinkManager->getTypes());

        $externalLinkManager->register($webLinkProvider);
        $externalLinkManager->register($attachmentLinkProvider);

        $this->assertEquals(
            array(ExternalLinkManager::TYPE_AUTO => 'Auto', 'attachment' => 'Attachment', 'weblink' => 'Web Link'),
            $externalLinkManager->getTypes()
        );
    }

    public function testGetDependencyLabel()
    {
        $externalLinkManager = new ExternalLinkManager($this->container);
        $webLinkProvider = new WebLinkProvider($this->container);
        $attachmentLinkProvider = new AttachmentLinkProvider($this->container);

        $externalLinkManager->register($webLinkProvider);
        $externalLinkManager->register($attachmentLinkProvider);

        $this->assertSame('Related', $externalLinkManager->getDependencyLabel($webLinkProvider->getType(), 'related'));
        $this->assertSame('custom', $externalLinkManager->getDependencyLabel($webLinkProvider->getType(), 'custom'));
    }

    public function testFindProviderNotFound()
    {
        $externalLinkManager = new ExternalLinkManager($this->container);
        $webLinkProvider = new WebLinkProvider($this->container);
        $attachmentLinkProvider = new AttachmentLinkProvider($this->container);

        $externalLinkManager->register($webLinkProvider);
        $externalLinkManager->register($attachmentLinkProvider);

        $this->setExpectedException('\Kanboard\Core\ExternalLink\ExternalLinkProviderNotFound');
        $externalLinkManager->find();
    }

    public function testFindProvider()
    {
        $externalLinkManager = new ExternalLinkManager($this->container);
        $webLinkProvider = new WebLinkProvider($this->container);
        $attachmentLinkProvider = new AttachmentLinkProvider($this->container);

        $externalLinkManager->register($webLinkProvider);
        $externalLinkManager->register($attachmentLinkProvider);

        $externalLinkManager->setUserInput(array('text' => 'https://google.com/', 'type' => ExternalLinkManager::TYPE_AUTO));
        $this->assertSame($webLinkProvider, $externalLinkManager->find());

        $externalLinkManager->setUserInput(array('text' => 'https://google.com/file.pdf', 'type' => ExternalLinkManager::TYPE_AUTO));
        $this->assertSame($attachmentLinkProvider, $externalLinkManager->find());
    }

    public function testFindProviderWithSelectedType()
    {
        $externalLinkManager = new ExternalLinkManager($this->container);
        $webLinkProvider = new WebLinkProvider($this->container);
        $attachmentLinkProvider = new AttachmentLinkProvider($this->container);

        $externalLinkManager->register($webLinkProvider);
        $externalLinkManager->register($attachmentLinkProvider);

        $externalLinkManager->setUserInput(array('text' => 'https://google.com/', 'type' => $webLinkProvider->getType()));
        $this->assertSame($webLinkProvider, $externalLinkManager->find());

        $externalLinkManager->setUserInput(array('text' => 'https://google.com/file.pdf', 'type' => $attachmentLinkProvider->getType()));
        $this->assertSame($attachmentLinkProvider, $externalLinkManager->find());
    }

    public function testFindProviderWithSelectedTypeNotFound()
    {
        $externalLinkManager = new ExternalLinkManager($this->container);
        $webLinkProvider = new WebLinkProvider($this->container);
        $attachmentLinkProvider = new AttachmentLinkProvider($this->container);

        $externalLinkManager->register($webLinkProvider);
        $externalLinkManager->register($attachmentLinkProvider);

        $this->setExpectedException('\Kanboard\Core\ExternalLink\ExternalLinkProviderNotFound');
        $externalLinkManager->setUserInput(array('text' => 'https://google.com/', 'type' => 'not found'));
        $externalLinkManager->find();
    }
}