Completed
Push — master ( 294148...bc0636 )
by
unknown
18s
created

testCKANClientConfigDecodesResultConditions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 19
rs 9.8333
c 0
b 0
f 0
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
        ], $config['fields'], 'DisplayConditions are decoded from stored JSON format');
63
    }
64
65
    /**
66
     * @param string $relativeLink
67
     * @param string $expected
68
     * @dataProvider basePathProvider
69
     */
70
    public function testGetBasePath($relativeLink, $expected)
71
    {
72
        /** @var CKANRegistryPage|PHPUnit_Framework_MockObject_MockObject $page */
73
        $page = $this->createMock(CKANRegistryPage::class);
74
        $page->expects($this->once())->method('RelativeLink')->willReturn($relativeLink);
75
76
        $controller = new CKANRegistryPageController($page);
77
78
        $this->assertSame($expected, $controller->getBasePath($page));
79
    }
80
81
    /**
82
     * @return array[]
83
     */
84
    public function basePathProvider()
85
    {
86
        return [
87
            ['/', '/'],
88
            ['/my-page', '/my-page'],
89
            ['my-page', '/my-page'],
90
            ['my-page/', '/my-page'],
91
            ['/my-page/', '/my-page'],
92
            ['/my-page/my-resources/', '/my-page/my-resources'],
93
        ];
94
    }
95
96
    public function testGetBasePathWithNoArgument()
97
    {
98
        /** @var CKANRegistryPage $page */
99
        $page = $this->objFromFixture(CKANRegistryPage::class, 'animal_centers');
100
101
        $controller = new CKANRegistryPageController($page);
102
103
        $this->assertSame('/', $controller->getBasePath());
104
    }
105
}
106