Completed
Push — master ( 882345...5b1789 )
by Nic
14s queued 10s
created

LocationTest::testUpdateWebsiteURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

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

221
        $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...
222
    }
223
}
224