LocationTest::testGetCoords()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 4
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Locator\Tests\Model;
4
5
use Dynamic\Locator\Tests\TestOnly\Extension\LocationExtension;
6
use Dynamic\Locator\Tests\TestOnlyModel\ExtendedLocation;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\Dev\SapphireTest;
9
use Dynamic\Locator\Location;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Security\Member;
12
13
/**
14
 * Class LocationTest
15
 * @package Dynamic\Locator\Tests\Model
16
 */
17
class LocationTest extends SapphireTest
18
{
19
    /**
20
     * @var string
21
     */
22
    protected static $fixture_file = 'locationfixture.yml';
23
24
    /**
25
     * @var array
26
     */
27
    protected static $extra_dataobjects = [
28
        ExtendedLocation::class,
29
    ];
30
31
    /**
32
     * @var array
33
     */
34
    protected static $required_extensions = [
35
        ExtendedLocation::class => [
36
            LocationExtension::class,
37
        ],
38
    ];
39
40
    /**
41
     *
42
     */
43
    public function testGetCoords()
44
    {
45
        $location = $this->objFromFixture(Location::class, 'dynamic');
46
        $coords = ((int)$location->Lat != 0 && (int)$location->Lng != 0) ? 'true' : 'false';
47
        $this->assertEquals($coords, $location->getCoords());
48
    }
49
50
    /**
51
     *
52
     */
53
    public function testFieldLabels()
54
    {
55
        $this->markTestSkipped();
56
        // Link and File tracking display as "Tracking" in SS 4.2 & 4.3, "Tracking" in 4.4
57
        $location = $this->objFromFixture(Location::class, 'dynamic');
58
        $labels = $location->FieldLabels();
59
        $expected = [
60
            'Title' => 'Name',
61
            'Featured' => 'Featured',
62
            'Website' => 'Website',
63
            'Phone' => 'Phone',
64
            'Email' => 'Email',
65
            'Fax' => 'Fax',
66
            'Address' => 'Address',
67
            'City' => 'City',
68
            'State' => 'State',
69
            'PostalCode' => 'Postal Code',
70
            'Country' => 'Country',
71
            'Lat' => 'Lat',
72
            'Lng' => 'Lng',
73
            'Categories' => 'Categories',
74
            'Category.Name' => 'Category',
75
            'Category.ID' => 'Category',
76
            'Featured.NiceAsBoolean' => 'Featured',
77
            'Import_ID' => 'Import_ID',
78
            'Version' => 'Version',
79
            'Versions' => 'Versions',
80
            'Address2' => 'Address2',
81
            'LinkTracking' => 'Link tracking',
82
            'FileTracking' => 'File tracking',
83
        ];
84
        $this->assertEquals($expected, $labels);
85
    }
86
87
    /**
88
     *
89
     */
90
    public function testGetCMSFields()
91
    {
92
        $object = new Location();
93
        $fieldset = $object->getCMSFields();
94
        $this->assertinstanceOf(FieldList::class, $fieldset);
95
    }
96
97
    /**
98
     *
99
     */
100
    public function testCanView()
101
    {
102
        $object = $this->objFromFixture(Location::class, 'dynamic');
103
        $object->write();
104
105
        $this->assertTrue($object->canView());
106
107
        $nullMember = Member::create();
108
        $nullMember->write();
109
110
        $this->assertTrue($object->canView($nullMember));
111
112
        $nullMember->delete();
113
    }
114
115
    /**
116
     *
117
     */
118
    public function testCanEdit()
119
    {
120
        $object = $this->objFromFixture(Location::class, 'dynamic');
121
        $object->write();
122
123
        $objectID = $object->ID;
124
125
        //test permissions per permission setting
126
        $create = $this->objFromFixture(Member::class, 'locationcreate');
127
        $edit = $this->objFromFixture(Member::class, 'locationedit');
128
        $delete = $this->objFromFixture(Member::class, 'locationdelete');
129
130
        $originalTitle = $object->Title;
131
        $this->assertEquals($originalTitle, 'Dynamic, Inc.');
132
133
        $this->assertTrue($object->canEdit($edit));
134
        $this->assertFalse($object->canEdit($create));
135
        $this->assertFalse($object->canEdit($delete));
136
137
        $object->Title = 'Changed Title';
138
        $object->write();
139
140
        $testEdit = Location::get()->byID($objectID);
141
        $this->assertEquals($testEdit->Title, 'Changed Title');
142
    }
143
144
    /**
145
     *
146
     */
147
    public function testCanDelete()
148
    {
149
        $object = $this->objFromFixture(Location::class, 'dynamic');
150
        $object->write();
151
152
        //test permissions per permission setting
153
        $create = $this->objFromFixture(Member::class, 'locationcreate');
154
        $edit = $this->objFromFixture(Member::class, 'locationedit');
155
        $delete = $this->objFromFixture(Member::class, 'locationdelete');
156
157
        $this->assertTrue($object->canDelete($delete));
158
        $this->assertFalse($object->canDelete($create));
159
        $this->assertFalse($object->canDelete($edit));
160
161
        $checkObject = $object;
162
        $object->delete();
163
164
        $this->assertEquals($checkObject->ID, 0);
165
    }
166
167
    /**
168
     *
169
     */
170
    public function testCanCreate()
171
    {
172
        $object = singleton(Location::class);
173
174
        //test permissions per permission setting
175
        $create = $this->objFromFixture(Member::class, 'locationcreate');
176
        $edit = $this->objFromFixture(Member::class, 'locationedit');
177
        $delete = $this->objFromFixture(Member::class, 'locationdelete');
178
179
        $this->assertTrue($object->canCreate($create));
180
        $this->assertFalse($object->canCreate($edit));
181
        $this->assertFalse($object->canCreate($delete));
182
183
        $nullMember = Member::create();
184
        $nullMember->write();
185
        $this->assertFalse($object->canCreate($nullMember));
186
187
        $nullMember->delete();
188
    }
189
190
    /**
191
     *
192
     */
193
    public function testProvidePermissions()
194
    {
195
        $object = Location::create();
196
        $expected = [
197
            'Location_EDIT' => 'Edit a Location',
198
            'Location_DELETE' => 'Delete a Location',
199
            'Location_CREATE' => 'Create a Location',
200
        ];
201
        $this->assertEquals($expected, $object->providePermissions());
202
    }
203
204
    /**
205
     *
206
     */
207
    public function testUpdateWebsiteURL()
208
    {
209
        $location = $this->objFromFixture(Location::class, 'dynamic');
210
211
        // Create unsaved raw duplicate
212
        $map = $location->toMap();
213
        unset($map['Created']);
214
        /** @var static $clone */
215
        $clone = Injector::inst()->create(ExtendedLocation::class, $map);
216
        $clone->ID = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
217
218
        $this->assertEquals('http://www.dynamicagency.com', $location->getWebsiteURL());
219
        $this->assertEquals('foo', $clone->getWebsiteURL());
0 ignored issues
show
Bug introduced by
The method getWebsiteURL() does not exist on Dynamic\Locator\Tests\Model\LocationTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

219
        $this->assertEquals('foo', $clone->/** @scrutinizer ignore-call */ getWebsiteURL());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
220
    }
221
}
222