testCKANClientConfigDecodesResultConditions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 20
rs 9.7998
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Tests\Page;
4
5
use PHPUnit_Framework_MockObject_MockObject;
6
use SilverStripe\CKANRegistry\Page\CKANRegistryPage;
7
use SilverStripe\CKANRegistry\Page\CKANRegistryPageController;
8
use SilverStripe\CKANRegistry\Service\ResourcePopulatorInterface;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\SapphireTest;
11
12
class CKANRegistryPageControllerTest extends SapphireTest
13
{
14
    protected static $fixture_file = 'CKANRegistryPageControllerTest.yml';
15
16
    protected function setUp()
17
    {
18
        // Mock the field populator, in case an action we perform in a unit test tries to contact the mock API.
19
        // Done before parent::setUp() so write hooks don't run during fixture population.
20
        $populator = $this->createMock(ResourcePopulatorInterface::class);
21
        Injector::inst()->registerService($populator, ResourcePopulatorInterface::class);
22
23
        parent::setUp();
24
    }
25
26
    public function testGetCKANClientConfig()
27
    {
28
        /** @var CKANRegistryPage $page */
29
        $page = $this->objFromFixture(CKANRegistryPage::class, 'animal_centers');
30
31
        $controller = new CKANRegistryPageController($page);
32
        $config = $controller->getCKANClientConfig($page);
33
34
        $this->assertSame([
35
            'endpoint' => 'https://example.com/ckan',
36
            'dataset' => 'animal-centers',
37
            'identifier' => '123-456',
38
        ], $config['spec'], 'CKAN endpoint specification should be provided');
39
        $this->assertSame('Animal Centers', $config['name'], 'Name should be provided');
40
        $this->assertSame('Vet Clinics', $config['resourceName'], 'Resource name should be provided');
41
        $this->assertSame('/animal-centers', $config['basePath'], 'Without a page, basePath should be root');
42
    }
43
44
    public function testCKANClientConfigDecodesResultConditions()
45
    {
46
        /** @var CKANRegistryPage $page */
47
        $page = $this->objFromFixture(CKANRegistryPage::class, 'animal_centers');
48
49
        $controller = new CKANRegistryPageController($page);
50
        $config = $controller->getCKANClientConfig($page);
51
52
        $this->assertNotEmpty($config['fields']);
53
        $this->assertContains([
54
            'OriginalLabel' => 'city',
55
            'ReadableLabel' => 'City',
56
            'ShowInResultsView' => 1,
57
            'ShowInDetailView' => 1,
58
            'DisplayConditions' => [
59
                ['match-select' => '1', 'match-text' => 'Auckland']
60
            ],
61
            'RemoveDuplicates' => 0,
62
            'Type' => 'text',
63
        ], $config['fields'], 'DisplayConditions are decoded from stored JSON format');
64
    }
65
66
    /**
67
     * @param string $relativeLink
68
     * @param string $expected
69
     * @dataProvider basePathProvider
70
     */
71
    public function testGetBasePath($relativeLink, $expected)
72
    {
73
        /** @var CKANRegistryPage|PHPUnit_Framework_MockObject_MockObject $page */
74
        $page = $this->createMock(CKANRegistryPage::class);
75
        $page->expects($this->once())->method('RelativeLink')->willReturn($relativeLink);
76
77
        $controller = new CKANRegistryPageController($page);
78
79
        $this->assertSame($expected, $controller->getBasePath($page));
80
    }
81
82
    /**
83
     * @return array[]
84
     */
85
    public function basePathProvider()
86
    {
87
        return [
88
            ['/', '/'],
89
            ['/my-page', '/my-page'],
90
            ['my-page', '/my-page'],
91
            ['my-page/', '/my-page'],
92
            ['/my-page/', '/my-page'],
93
            ['/my-page/my-resources/', '/my-page/my-resources'],
94
        ];
95
    }
96
97
    public function testGetBasePathWithNoArgument()
98
    {
99
        /** @var CKANRegistryPage $page */
100
        $page = $this->objFromFixture(CKANRegistryPage::class, 'animal_centers');
101
102
        $controller = new CKANRegistryPageController($page);
103
104
        $this->assertSame('/', $controller->getBasePath());
105
    }
106
}
107