Passed
Pull Request — master (#56)
by Robbie
02:25
created

testSetValueWithNoDataSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Tests\Forms;
4
5
use InvalidArgumentException;
6
use SilverStripe\CKANRegistry\Forms\ResourceLocatorField;
7
use SilverStripe\CKANRegistry\Model\Resource;
8
use SilverStripe\CKANRegistry\Page\CKANRegistryPage;
9
use SilverStripe\CKANRegistry\Service\ResourceFieldPopulatorInterface;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Dev\SapphireTest;
12
use stdClass;
13
14
class ResourceLocatorFieldTest extends SapphireTest
15
{
16
    protected $usesDatabase = true;
17
18
    /**
19
     * @var ResourceLocatorField
20
     */
21
    protected $field;
22
23
    protected function setUp()
24
    {
25
        parent::setUp();
26
27
        $this->field = new ResourceLocatorField('my-field');
28
29
        $this->field
30
            ->setEndpointFieldName('Endpoint')
31
            ->setDatasetFieldName('DataSet')
32
            ->setResourceFieldName('Identifier');
33
34
        // Mock the field populator, in case an action we perform in a unit test tries to contact the mock API
35
        $populator = $this->createMock(ResourceFieldPopulatorInterface::class);
36
        Injector::inst()->registerService($populator, ResourceFieldPopulatorInterface::class);
37
    }
38
39
    public function testConstruct()
40
    {
41
        $field = new ResourceLocatorField('my-field', 'My field', null, 'http://example.com');
42
43
        $this->assertSame('http://example.com', $field->getDefaultEndpoint());
44
        $this->assertContains('Connect to a data source from', $field->getDescription());
45
        $this->assertTrue(
46
            $field->hasClass('ckan-resource-locator__container'),
47
            'Field class name is required for React scaffold'
48
        );
49
    }
50
51
    public function testSetValueWithArray()
52
    {
53
        $this->field->setValue(['foo' => 'bar']);
54
        $this->assertSame(['foo' => 'bar'], $this->field->Value(), 'Array values should pass through');
55
    }
56
57
    public function testSetValueWithNonDataObject()
58
    {
59
        $value = new stdClass();
60
        $value->foo = 'bar';
61
        $this->field->setValue($value);
62
63
        $this->assertNull($this->field->Value(), 'Non DataObject instances should set a null value');
64
    }
65
66
    public function testSetValueWithNoDataSet()
67
    {
68
        $this->field
69
            ->setDatasetFieldName('NonExistentField')
70
            ->setValue($this->getResource());
71
72
        $this->assertNull($this->field->Value(), 'Missing data sets should set a null value');
73
    }
74
75
    public function testSetValueWithNoEndpointFallsBackToDefaultEndpoint()
76
    {
77
        $this->field
78
            ->setEndpointFieldName('NonExistentField')
79
            ->setDefaultEndpoint('http://example.com')
80
            ->setValue($this->getResource());
81
82
        $this->assertContains(
83
            'http://example.com',
84
            $this->field->Value(),
85
            'Missing endpoint data on the model should fall back to the configured default endpoint'
86
        );
87
    }
88
89
    public function testSetValueWithNoEndpointAndNoDefaultReturnsNull()
90
    {
91
        ResourceLocatorField::config()->set('default_endpoint', '');
92
93
        $this->field
94
            ->setEndpointFieldName('NonExistentField')
95
            ->setDefaultEndpoint(null)
96
            ->setValue($this->getResource());
97
98
        $this->assertNull(
99
            $this->field->Value(),
100
            'Missing endpoint data on the model, and missing default endpoint should return null'
101
        );
102
    }
103
104
    public function testSetValueWithExpectedDataStructure()
105
    {
106
        $this->field->setValue($this->getResource());
107
        $result = $this->field->Value();
108
109
        $this->assertContains('http://example.com', $result);
110
        $this->assertContains('foo-bar', $result);
111
        $this->assertContains('123-456', $result);
112
    }
113
114
    public function testSetSubmittedValue()
115
    {
116
        $value = json_encode([
117
            'endpoint' => 'https://www.silverstripe.org',
118
            'dataset' => 'monkeys',
119
            'resource' => 'feeding',
120
        ]);
121
        $this->field->setSubmittedValue($value);
122
123
        $this->assertContains('https://www.silverstripe.org', $this->field->Value());
124
    }
125
126
    public function testDataValue()
127
    {
128
        $this->field->setValue($this->getResource());
129
        $result = $this->field->dataValue();
130
131
        $this->assertJson($result, 'dataValue() should return JSON');
132
        $this->assertContains('foo-bar', $result, 'Serialised result should contain value');
133
    }
134
135
    public function testSaveIntoWithNoNameReturnsEarly()
136
    {
137
        $this->field->setName(null);
138
        $this->assertNull($this->field->saveInto($this->getPage()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->field->saveInto($this->getPage()) targeting SilverStripe\CKANRegistr...ocatorField::saveInto() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
139
    }
140
141
    /**
142
     * @expectedException InvalidArgumentException
143
     * @expectedExceptionMessageRegExp /Could not determine where to save the value of/
144
     */
145
    public function testSaveIntoWithInvalidRelationshipNameThrowsException()
146
    {
147
        $this->field->setName('UnicornRelationship');
148
        $this->field->saveInto($this->getPage());
149
    }
150
151
    public function testSaveIntoSetsRelationshipFields()
152
    {
153
        $page = $this->getPage();
154
        $this->field->setName('DataResource');
155
        $this->field->setValue($this->getResource());
156
        $this->field->saveInto($page);
157
158
        $this->assertNotEmpty($page->DataResourceID);
159
        $this->assertSame('http://example.com', $page->DataResource()->Endpoint);
160
        $this->assertSame('foo-bar', $page->DataResource()->DataSet);
161
        $this->assertSame('123-456', $page->DataResource()->Identifier);
162
    }
163
164
    public function testDefaultEndpointUsesConfigIfNotProvided()
165
    {
166
        $this->field->setDefaultEndpoint('foo');
167
        $this->assertSame('foo', $this->field->getDefaultEndpoint());
168
169
        $this->field->setDefaultEndpoint(null);
170
        ResourceLocatorField::config()->set('default_endpoint', 'bar');
171
        $this->assertSame('bar', $this->field->getDefaultEndpoint());
172
    }
173
174
    public function testSiteNameUsesTranslatedDefaultIfNotProvided()
175
    {
176
        $this->field->setSiteName('My cool website');
177
        $this->assertSame('My cool website', $this->field->getSiteName());
178
179
        $this->field->setSiteName(null);
180
        $this->assertSame('a CKAN website', $this->field->getSiteName());
181
    }
182
183
    /**
184
     * Returns a Resource object for testing values in the field
185
     *
186
     * @return Resource
187
     */
188
    protected function getResource()
189
    {
190
        $resource = new Resource();
191
        $resource->Endpoint = 'http://example.com';
192
        $resource->DataSet = 'foo-bar';
193
        $resource->Identifier = '123-456';
194
        return $resource;
195
    }
196
197
    /**
198
     * Returns a CKAN Page object for testing values in the field
199
     *
200
     * @return CKANRegistryPage
201
     */
202
    protected function getPage()
203
    {
204
        $page = new CKANRegistryPage();
205
        $page->Title = 'My CKAN Page';
206
        return $page;
207
    }
208
}
209