Completed
Push — master ( cc20d7...7a07c8 )
by Nic
03:04
created
tests/LocationTest.php 1 patch
Indentation   +146 added lines, -146 removed lines patch added patch discarded remove patch
@@ -3,150 +3,150 @@
 block discarded – undo
3 3
 class LocationTest extends SapphireTest
4 4
 {
5 5
 
6
-    protected static $fixture_file = 'locator/tests/Locator_Test.yml';
7
-
8
-    public function testGetCoords()
9
-    {
10
-        $location = $this->objFromFixture('Location', 'dynamic');
11
-
12
-        $coords = ((int)$location->Lat != 0 && (int)$location->Lng != 0) ? 'true' : 'false';
13
-
14
-        $this->assertEquals($coords, $location->getCoords());
15
-    }
16
-
17
-    public function testFieldLabels()
18
-    {
19
-        $location = $this->objFromFixture('Location', 'dynamic');
20
-        $labels = $location->FieldLabels();
21
-        $expected = array(
22
-            'Title' => 'Name',
23
-            'Featured' => 'Featured',
24
-            'Website' => 'Website',
25
-            'Phone' => 'Phone',
26
-            'Email' => 'Email',
27
-            'EmailAddress' => 'Email Address',
28
-            'ShowInLocator' => 'Show',
29
-            'Address' => 'Address',
30
-            'Suburb' => 'City',
31
-            'State' => 'State',
32
-            'Postcode' => 'Postal Code',
33
-            'Country' => 'Country',
34
-            'Lat' => 'Lat',
35
-            'Lng' => 'Lng',
36
-            'Category' => 'Category',
37
-            'ShowInLocator.NiceAsBoolean' => 'Show',
38
-            'Category.Name' => 'Category',
39
-            'Category.ID' => 'Category',
40
-            'Featured.NiceAsBoolean' => 'Featured',
41
-            'Coords' => 'Coords',
42
-        );
43
-        $this->assertEquals($expected, $labels);
44
-    }
45
-
46
-    public function testGetCMSFields()
47
-    {
48
-        $object = new Location();
49
-        $fieldset = $object->getCMSFields();
50
-        $this->assertTrue(is_a($fieldset, 'FieldList'));
51
-    }
52
-
53
-    public function testValidate()
54
-    {
55
-    }
56
-
57
-    public function testEmailAddress()
58
-    {
59
-    }
60
-
61
-    public function testCanView()
62
-    {
63
-        $object = $this->objFromFixture('Location', 'dynamic');
64
-        $object->write();
65
-
66
-        $this->assertTrue($object->canView());
67
-
68
-        $nullMember = Member::create();
69
-        $nullMember->write();
70
-
71
-        $this->assertTrue($object->canView($nullMember));
72
-
73
-        $nullMember->delete();
74
-    }
75
-
76
-    public function testCanEdit()
77
-    {
78
-        $object = $this->objFromFixture('Location', 'dynamic');
79
-        $object->write();
80
-
81
-        $objectID = $object->ID;
82
-
83
-        //test permissions per permission setting
84
-        $create = $this->objFromFixture('Member', 'locationcreate');
85
-        $edit = $this->objFromFixture('Member', 'locationedit');
86
-        $delete = $this->objFromFixture('Member', 'locationdelete');
87
-
88
-        $originalTitle = $object->Title;
89
-        $this->assertEquals($originalTitle, 'Dynamic, Inc.');
90
-
91
-        $this->assertTrue($object->canEdit($edit));
92
-        $this->assertFalse($object->canEdit($create));
93
-        $this->assertFalse($object->canEdit($delete));
94
-
95
-        $object->Title = 'Changed Title';
96
-        $object->write();
97
-
98
-        $testEdit = Location::get()->byID($objectID);
99
-        $this->assertEquals($testEdit->Title, 'Changed Title');
100
-    }
101
-
102
-    public function testCanDelete()
103
-    {
104
-        $object = $this->objFromFixture('Location', 'dynamic');
105
-        $object->write();
106
-
107
-        //test permissions per permission setting
108
-        $create = $this->objFromFixture('Member', 'locationcreate');
109
-        $edit = $this->objFromFixture('Member', 'locationedit');
110
-        $delete = $this->objFromFixture('Member', 'locationdelete');
111
-
112
-        $this->assertTrue($object->canDelete($delete));
113
-        $this->assertFalse($object->canDelete($create));
114
-        $this->assertFalse($object->canDelete($edit));
115
-
116
-        $checkObject = $object;
117
-        $object->delete();
118
-
119
-        $this->assertEquals($checkObject->ID, 0);
120
-    }
121
-
122
-    public function testCanCreate()
123
-    {
124
-        $object = singleton('Location');
125
-
126
-        //test permissions per permission setting
127
-        $create = $this->objFromFixture('Member', 'locationcreate');
128
-        $edit = $this->objFromFixture('Member', 'locationedit');
129
-        $delete = $this->objFromFixture('Member', 'locationdelete');
130
-
131
-        $this->assertTrue($object->canCreate($create));
132
-        $this->assertFalse($object->canCreate($edit));
133
-        $this->assertFalse($object->canCreate($delete));
134
-
135
-        $nullMember = Member::create();
136
-        $nullMember->write();
137
-        $this->assertFalse($object->canCreate($nullMember));
138
-
139
-        $nullMember->delete();
140
-    }
141
-
142
-    public function testProvidePermissions()
143
-    {
144
-        $object = Location::create();
145
-        $expected = array(
146
-            'Location_EDIT' => 'Edit a Location',
147
-            'Location_DELETE' => 'Delete a Location',
148
-            'Location_CREATE' => 'Create a Location',
149
-        );
150
-        $this->assertEquals($expected, $object->providePermissions());
151
-    }
6
+	protected static $fixture_file = 'locator/tests/Locator_Test.yml';
7
+
8
+	public function testGetCoords()
9
+	{
10
+		$location = $this->objFromFixture('Location', 'dynamic');
11
+
12
+		$coords = ((int)$location->Lat != 0 && (int)$location->Lng != 0) ? 'true' : 'false';
13
+
14
+		$this->assertEquals($coords, $location->getCoords());
15
+	}
16
+
17
+	public function testFieldLabels()
18
+	{
19
+		$location = $this->objFromFixture('Location', 'dynamic');
20
+		$labels = $location->FieldLabels();
21
+		$expected = array(
22
+			'Title' => 'Name',
23
+			'Featured' => 'Featured',
24
+			'Website' => 'Website',
25
+			'Phone' => 'Phone',
26
+			'Email' => 'Email',
27
+			'EmailAddress' => 'Email Address',
28
+			'ShowInLocator' => 'Show',
29
+			'Address' => 'Address',
30
+			'Suburb' => 'City',
31
+			'State' => 'State',
32
+			'Postcode' => 'Postal Code',
33
+			'Country' => 'Country',
34
+			'Lat' => 'Lat',
35
+			'Lng' => 'Lng',
36
+			'Category' => 'Category',
37
+			'ShowInLocator.NiceAsBoolean' => 'Show',
38
+			'Category.Name' => 'Category',
39
+			'Category.ID' => 'Category',
40
+			'Featured.NiceAsBoolean' => 'Featured',
41
+			'Coords' => 'Coords',
42
+		);
43
+		$this->assertEquals($expected, $labels);
44
+	}
45
+
46
+	public function testGetCMSFields()
47
+	{
48
+		$object = new Location();
49
+		$fieldset = $object->getCMSFields();
50
+		$this->assertTrue(is_a($fieldset, 'FieldList'));
51
+	}
52
+
53
+	public function testValidate()
54
+	{
55
+	}
56
+
57
+	public function testEmailAddress()
58
+	{
59
+	}
60
+
61
+	public function testCanView()
62
+	{
63
+		$object = $this->objFromFixture('Location', 'dynamic');
64
+		$object->write();
65
+
66
+		$this->assertTrue($object->canView());
67
+
68
+		$nullMember = Member::create();
69
+		$nullMember->write();
70
+
71
+		$this->assertTrue($object->canView($nullMember));
72
+
73
+		$nullMember->delete();
74
+	}
75
+
76
+	public function testCanEdit()
77
+	{
78
+		$object = $this->objFromFixture('Location', 'dynamic');
79
+		$object->write();
80
+
81
+		$objectID = $object->ID;
82
+
83
+		//test permissions per permission setting
84
+		$create = $this->objFromFixture('Member', 'locationcreate');
85
+		$edit = $this->objFromFixture('Member', 'locationedit');
86
+		$delete = $this->objFromFixture('Member', 'locationdelete');
87
+
88
+		$originalTitle = $object->Title;
89
+		$this->assertEquals($originalTitle, 'Dynamic, Inc.');
90
+
91
+		$this->assertTrue($object->canEdit($edit));
92
+		$this->assertFalse($object->canEdit($create));
93
+		$this->assertFalse($object->canEdit($delete));
94
+
95
+		$object->Title = 'Changed Title';
96
+		$object->write();
97
+
98
+		$testEdit = Location::get()->byID($objectID);
99
+		$this->assertEquals($testEdit->Title, 'Changed Title');
100
+	}
101
+
102
+	public function testCanDelete()
103
+	{
104
+		$object = $this->objFromFixture('Location', 'dynamic');
105
+		$object->write();
106
+
107
+		//test permissions per permission setting
108
+		$create = $this->objFromFixture('Member', 'locationcreate');
109
+		$edit = $this->objFromFixture('Member', 'locationedit');
110
+		$delete = $this->objFromFixture('Member', 'locationdelete');
111
+
112
+		$this->assertTrue($object->canDelete($delete));
113
+		$this->assertFalse($object->canDelete($create));
114
+		$this->assertFalse($object->canDelete($edit));
115
+
116
+		$checkObject = $object;
117
+		$object->delete();
118
+
119
+		$this->assertEquals($checkObject->ID, 0);
120
+	}
121
+
122
+	public function testCanCreate()
123
+	{
124
+		$object = singleton('Location');
125
+
126
+		//test permissions per permission setting
127
+		$create = $this->objFromFixture('Member', 'locationcreate');
128
+		$edit = $this->objFromFixture('Member', 'locationedit');
129
+		$delete = $this->objFromFixture('Member', 'locationdelete');
130
+
131
+		$this->assertTrue($object->canCreate($create));
132
+		$this->assertFalse($object->canCreate($edit));
133
+		$this->assertFalse($object->canCreate($delete));
134
+
135
+		$nullMember = Member::create();
136
+		$nullMember->write();
137
+		$this->assertFalse($object->canCreate($nullMember));
138
+
139
+		$nullMember->delete();
140
+	}
141
+
142
+	public function testProvidePermissions()
143
+	{
144
+		$object = Location::create();
145
+		$expected = array(
146
+			'Location_EDIT' => 'Edit a Location',
147
+			'Location_DELETE' => 'Delete a Location',
148
+			'Location_CREATE' => 'Create a Location',
149
+		);
150
+		$this->assertEquals($expected, $object->providePermissions());
151
+	}
152 152
 }
Please login to merge, or discard this patch.
code/Location.php 1 patch
Indentation   +181 added lines, -181 removed lines patch added patch discarded remove patch
@@ -2,185 +2,185 @@
 block discarded – undo
2 2
 
3 3
 class Location extends DataObject implements PermissionProvider
4 4
 {
5
-    private static $db = array(
6
-        'Title' => 'Varchar(255)',
7
-        'Featured' => 'Boolean',
8
-        'Website' => 'Varchar(255)',
9
-        'Phone' => 'Varchar(40)',
10
-        'Email' => 'Varchar(255)',
11
-        'EmailAddress' => 'Varchar(255)',
12
-        'ShowInLocator' => 'Boolean',
13
-    );
14
-
15
-    private static $has_one = array(
16
-        'Category' => 'LocationCategory',
17
-    );
18
-
19
-    private static $casting = array(
20
-        'distance' => 'Int',
21
-    );
22
-
23
-    private static $default_sort = 'Title';
24
-
25
-    private static $defaults = array(
26
-        'ShowInLocator' => true,
27
-    );
28
-
29
-    private static $singular_name = 'Location';
30
-    private static $plural_name = 'Locations';
31
-
32
-    // api access via Restful Server module
33
-    private static $api_access = true;
34
-
35
-    // search fields for Model Admin
36
-    private static $searchable_fields = array(
37
-        'Title',
38
-        'Address',
39
-        'Suburb',
40
-        'State',
41
-        'Postcode',
42
-        'Country',
43
-        'Website',
44
-        'Phone',
45
-        'Email',
46
-        'Category.ID',
47
-        'ShowInLocator',
48
-        'Featured',
49
-    );
50
-
51
-    // columns for grid field
52
-    private static $summary_fields = array(
53
-        'Title',
54
-        'Address',
55
-        'Suburb',
56
-        'State',
57
-        'Postcode',
58
-        'Country',
59
-        'Category.Name',
60
-        'ShowInLocator.NiceAsBoolean',
61
-        'Featured.NiceAsBoolean',
62
-        'Coords',
63
-    );
64
-
65
-    // Coords status for $summary_fields
66
-    public function getCoords()
67
-    {
68
-        return ($this->Lat != 0 && $this->Lng != 0) ? 'true' : 'false';
69
-    }
70
-
71
-    // custom labels for fields
72
-    public function fieldLabels($includerelations = true)
73
-    {
74
-        $labels = parent::fieldLabels();
75
-
76
-        $labels['Title'] = 'Name';
77
-        $labels['Suburb'] = 'City';
78
-        $labels['Postcode'] = 'Postal Code';
79
-        $labels['ShowInLocator'] = 'Show';
80
-        $labels['ShowInLocator.NiceAsBoolean'] = 'Show';
81
-        $labels['Category.Name'] = 'Category';
82
-        $labels['Category.ID'] = 'Category';
83
-        $labels['Email'] = 'Email';
84
-        $labels['Featured.NiceAsBoolean'] = 'Featured';
85
-        $labels['Coords'] = 'Coords';
86
-
87
-        return $labels;
88
-    }
89
-
90
-    public function getCMSFields()
91
-    {
92
-        $fields = FieldList::create(
93
-            new TabSet(
94
-                $name = 'Root',
95
-                new Tab(
96
-                    $title = 'Main',
97
-                    HeaderField::create('ContactHD', 'Contact Information'),
98
-                    TextField::create('Title', 'Name'),
99
-                    TextField::create('Phone'),
100
-                    EmailField::create('Email', 'Email'),
101
-                    TextField::create('Website')
102
-                        ->setAttribute('placeholder', 'http://'),
103
-                    DropDownField::create('CategoryID', 'Category', LocationCategory::get()->map('ID', 'Title'))
104
-                        ->setEmptyString('-- select --'),
105
-                    CheckboxField::create('ShowInLocator', 'Show in results')
106
-                        ->setDescription('Location will be included in results list'),
107
-                    CheckboxField::create('Featured')
108
-                        ->setDescription('Location will show at/near the top of the results list')
109
-                )
110
-            )
111
-        );
112
-
113
-        // allow to be extended via DataExtension
114
-        $this->extend('updateCMSFields', $fields);
115
-
116
-        // override Suburb field name
117
-        $fields->dataFieldByName('Suburb')->setTitle('City');
118
-
119
-        return $fields;
120
-    }
121
-
122
-    public function validate()
123
-    {
124
-        $result = parent::validate();
125
-
126
-        return $result;
127
-    }
128
-
129
-    public function EmailAddress()
130
-    {
131
-        Deprecation::notice('3.0', 'Use "$Email" instead.');
132
-        if ($this->Email) {
133
-            return $this->Email;
134
-        } elseif ($this->EmailAddress) {
135
-            return $this->EmailAddress;
136
-        }
137
-
138
-        return false;
139
-    }
140
-
141
-    /**
142
-     * @param null|Member $member
143
-     *
144
-     * @return bool
145
-     */
146
-    public function canView($member = null)
147
-    {
148
-        return true;
149
-    }
150
-
151
-    /**
152
-     * @param null|Member $member
153
-     * @return bool|int
154
-     */
155
-    public function canEdit($member = null)
156
-    {
157
-        return Permission::check('Location_EDIT', 'any', $member);
158
-    }
159
-
160
-    /**
161
-     * @param null|Member $member
162
-     * @return bool|int
163
-     */
164
-    public function canDelete($member = null)
165
-    {
166
-        return Permission::check('Location_DELETE', 'any', $member);
167
-    }
168
-
169
-    /**
170
-     * @param null|Member $member
171
-     * @return bool|int
172
-     */
173
-    public function canCreate($member = null)
174
-    {
175
-        return Permission::check('Location_CREATE', 'any', $member);
176
-    }
177
-
178
-    public function providePermissions()
179
-    {
180
-        return array(
181
-            'Location_EDIT' => 'Edit a Location',
182
-            'Location_DELETE' => 'Delete a Location',
183
-            'Location_CREATE' => 'Create a Location',
184
-        );
185
-    }
5
+	private static $db = array(
6
+		'Title' => 'Varchar(255)',
7
+		'Featured' => 'Boolean',
8
+		'Website' => 'Varchar(255)',
9
+		'Phone' => 'Varchar(40)',
10
+		'Email' => 'Varchar(255)',
11
+		'EmailAddress' => 'Varchar(255)',
12
+		'ShowInLocator' => 'Boolean',
13
+	);
14
+
15
+	private static $has_one = array(
16
+		'Category' => 'LocationCategory',
17
+	);
18
+
19
+	private static $casting = array(
20
+		'distance' => 'Int',
21
+	);
22
+
23
+	private static $default_sort = 'Title';
24
+
25
+	private static $defaults = array(
26
+		'ShowInLocator' => true,
27
+	);
28
+
29
+	private static $singular_name = 'Location';
30
+	private static $plural_name = 'Locations';
31
+
32
+	// api access via Restful Server module
33
+	private static $api_access = true;
34
+
35
+	// search fields for Model Admin
36
+	private static $searchable_fields = array(
37
+		'Title',
38
+		'Address',
39
+		'Suburb',
40
+		'State',
41
+		'Postcode',
42
+		'Country',
43
+		'Website',
44
+		'Phone',
45
+		'Email',
46
+		'Category.ID',
47
+		'ShowInLocator',
48
+		'Featured',
49
+	);
50
+
51
+	// columns for grid field
52
+	private static $summary_fields = array(
53
+		'Title',
54
+		'Address',
55
+		'Suburb',
56
+		'State',
57
+		'Postcode',
58
+		'Country',
59
+		'Category.Name',
60
+		'ShowInLocator.NiceAsBoolean',
61
+		'Featured.NiceAsBoolean',
62
+		'Coords',
63
+	);
64
+
65
+	// Coords status for $summary_fields
66
+	public function getCoords()
67
+	{
68
+		return ($this->Lat != 0 && $this->Lng != 0) ? 'true' : 'false';
69
+	}
70
+
71
+	// custom labels for fields
72
+	public function fieldLabels($includerelations = true)
73
+	{
74
+		$labels = parent::fieldLabels();
75
+
76
+		$labels['Title'] = 'Name';
77
+		$labels['Suburb'] = 'City';
78
+		$labels['Postcode'] = 'Postal Code';
79
+		$labels['ShowInLocator'] = 'Show';
80
+		$labels['ShowInLocator.NiceAsBoolean'] = 'Show';
81
+		$labels['Category.Name'] = 'Category';
82
+		$labels['Category.ID'] = 'Category';
83
+		$labels['Email'] = 'Email';
84
+		$labels['Featured.NiceAsBoolean'] = 'Featured';
85
+		$labels['Coords'] = 'Coords';
86
+
87
+		return $labels;
88
+	}
89
+
90
+	public function getCMSFields()
91
+	{
92
+		$fields = FieldList::create(
93
+			new TabSet(
94
+				$name = 'Root',
95
+				new Tab(
96
+					$title = 'Main',
97
+					HeaderField::create('ContactHD', 'Contact Information'),
98
+					TextField::create('Title', 'Name'),
99
+					TextField::create('Phone'),
100
+					EmailField::create('Email', 'Email'),
101
+					TextField::create('Website')
102
+						->setAttribute('placeholder', 'http://'),
103
+					DropDownField::create('CategoryID', 'Category', LocationCategory::get()->map('ID', 'Title'))
104
+						->setEmptyString('-- select --'),
105
+					CheckboxField::create('ShowInLocator', 'Show in results')
106
+						->setDescription('Location will be included in results list'),
107
+					CheckboxField::create('Featured')
108
+						->setDescription('Location will show at/near the top of the results list')
109
+				)
110
+			)
111
+		);
112
+
113
+		// allow to be extended via DataExtension
114
+		$this->extend('updateCMSFields', $fields);
115
+
116
+		// override Suburb field name
117
+		$fields->dataFieldByName('Suburb')->setTitle('City');
118
+
119
+		return $fields;
120
+	}
121
+
122
+	public function validate()
123
+	{
124
+		$result = parent::validate();
125
+
126
+		return $result;
127
+	}
128
+
129
+	public function EmailAddress()
130
+	{
131
+		Deprecation::notice('3.0', 'Use "$Email" instead.');
132
+		if ($this->Email) {
133
+			return $this->Email;
134
+		} elseif ($this->EmailAddress) {
135
+			return $this->EmailAddress;
136
+		}
137
+
138
+		return false;
139
+	}
140
+
141
+	/**
142
+	 * @param null|Member $member
143
+	 *
144
+	 * @return bool
145
+	 */
146
+	public function canView($member = null)
147
+	{
148
+		return true;
149
+	}
150
+
151
+	/**
152
+	 * @param null|Member $member
153
+	 * @return bool|int
154
+	 */
155
+	public function canEdit($member = null)
156
+	{
157
+		return Permission::check('Location_EDIT', 'any', $member);
158
+	}
159
+
160
+	/**
161
+	 * @param null|Member $member
162
+	 * @return bool|int
163
+	 */
164
+	public function canDelete($member = null)
165
+	{
166
+		return Permission::check('Location_DELETE', 'any', $member);
167
+	}
168
+
169
+	/**
170
+	 * @param null|Member $member
171
+	 * @return bool|int
172
+	 */
173
+	public function canCreate($member = null)
174
+	{
175
+		return Permission::check('Location_CREATE', 'any', $member);
176
+	}
177
+
178
+	public function providePermissions()
179
+	{
180
+		return array(
181
+			'Location_EDIT' => 'Edit a Location',
182
+			'Location_DELETE' => 'Delete a Location',
183
+			'Location_CREATE' => 'Create a Location',
184
+		);
185
+	}
186 186
 }
Please login to merge, or discard this patch.