1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Test cases for FlexSlider
|
6
|
*
|
7
|
* @author Mathew Winstone <mwinstone@coldfrontlabs.ca>
|
8
|
*/
|
9
|
|
10
|
|
11
|
class FlexsliderTestCase extends DrupalWebTestCase {
|
12
|
protected $admin_user;
|
13
|
protected $any_user;
|
14
|
|
15
|
public static function getInfo() {
|
16
|
return array(
|
17
|
'name' => t('FlexSlider tests'),
|
18
|
'description' => t('Test the FlexSlider presets, configuration options and permission controls.'),
|
19
|
'group' => t('FlexSlider'),
|
20
|
);
|
21
|
}
|
22
|
|
23
|
function setUp() {
|
24
|
parent::setUp('libraries', 'ctools', 'flexslider');
|
25
|
|
26
|
// Create users
|
27
|
$this->admin_user = $this->drupalCreateUser(array('administer flexslider'));
|
28
|
$this->any_user = $this->drupalCreateuser(array('access administration pages'));
|
29
|
}
|
30
|
|
31
|
function testAdminAccess() {
|
32
|
|
33
|
// Login as the admin user
|
34
|
$this->drupalLogin($this->admin_user);
|
35
|
|
36
|
// Load admin page
|
37
|
$this->drupalGet('admin/config/media/flexslider');
|
38
|
$this->assertResponse(200, t('Administrative permission allows access to administration page.'));
|
39
|
|
40
|
// Logout as admin user
|
41
|
$this->drupalLogout();
|
42
|
|
43
|
// Login as any user
|
44
|
$this->drupalLogin($this->any_user);
|
45
|
|
46
|
// Attempt to load admin page
|
47
|
$this->drupalGet('admin/config/media/flexslider');
|
48
|
$this->assertResponse(403, t('Regular users do not have access to administrative pages.'));
|
49
|
}
|
50
|
|
51
|
function testOptionSetCrud() {
|
52
|
// Login as the admin user
|
53
|
$this->drupalLogin($this->admin_user);
|
54
|
$testsets = array('testset', 'testset2');
|
55
|
|
56
|
foreach ($testsets as $name) {
|
57
|
// Create a new optionset with default settings
|
58
|
$optionset = flexslider_optionset_create(array('name' => $name));
|
59
|
$this->assertTrue($optionset->name == $name, t('Optionset object created: @name', array('@name' => $optionset->name)));
|
60
|
$this->assertFalse(empty($optionset->options), t('Create optionset works.'));
|
61
|
|
62
|
// Save the optionset to the database
|
63
|
$optionset = flexslider_optionset_save($optionset, TRUE);
|
64
|
$this->assertFalse(FALSE === $optionset, t('Optionset saved to database.'));
|
65
|
|
66
|
// Read the values from the database
|
67
|
$optionset = flexslider_optionset_load($name);
|
68
|
|
69
|
$this->assertTrue(is_object($optionset), t('Loaded option set.'));
|
70
|
$this->assertEqual($name, $optionset->name, t('Loaded name matches: @name', array('@name' => $optionset->name)));
|
71
|
|
72
|
$default_optionset = flexslider_optionset_create();
|
73
|
foreach ($default_optionset->options as $key => $value) {
|
74
|
$this->assertEqual($value, $optionset->options[$key], t('Option @option matches saved value.', array('@option' => $key)));
|
75
|
}
|
76
|
|
77
|
}
|
78
|
|
79
|
// Load all optionsets
|
80
|
$optionsets = flexslider_optionset_load_all();
|
81
|
$this->assertTrue(is_array($optionsets), t('Array of optionsets loaded'));
|
82
|
$this->assertTrue(count($optionsets) == 3, t('Proper number of optionsets loaded (two created, one default): 3'));
|
83
|
|
84
|
// Ensure they all loaded correctly
|
85
|
foreach ($optionsets as $optionset) {
|
86
|
$this->assertTrue(isset($optionset->name), t('Loaded optionsets have a defined machine name'));
|
87
|
$this->assertTrue(isset($optionset->title), t('Loaded optionsets have a defined human readable name (title)'));
|
88
|
$this->assertTrue(isset($optionset->options), t('Loaded optionsets have a defined array of options'));
|
89
|
}
|
90
|
|
91
|
// Update the optionset
|
92
|
$test_options = _flexslider_test_options();
|
93
|
$test_options = $test_options['valid'];
|
94
|
|
95
|
// Load one of the test option sets
|
96
|
$optionset = flexslider_optionset_load($testsets[0]);
|
97
|
|
98
|
// Change the settings
|
99
|
$optionset->options += $test_options['set2'];
|
100
|
|
101
|
// Save the updated values
|
102
|
$optionset = flexslider_optionset_save($optionset);
|
103
|
$this->assertFalse(FALSE == $optionset, t('Saved updates to optionset to database.'));
|
104
|
|
105
|
// Load the values from the database again
|
106
|
$optionset = flexslider_optionset_load($testsets[0]);
|
107
|
|
108
|
// Compare settings to the test options
|
109
|
foreach ($test_options['set2'] as $key => $value) {
|
110
|
$this->assertEqual($optionset->options[$key], $value, t('Saved value matches set value: @key', array('@key' => $key)));
|
111
|
}
|
112
|
|
113
|
// Delete the optionset
|
114
|
$this->assertTrue(flexslider_optionset_exists($optionset->name), t('Optionset exists and is ready to be deleted.'));
|
115
|
flexslider_optionset_delete($optionset->name);
|
116
|
|
117
|
// Ensure the delete is successfull
|
118
|
$this->assertFalse(flexslider_optionset_exists($optionset->name), t('Optionset successfully deleted: @name', array('@name' => $optionset->name)));
|
119
|
}
|
120
|
|
121
|
function testOptionSetForm() {
|
122
|
|
123
|
// Login with admin user
|
124
|
$this->drupalLogin($this->admin_user);
|
125
|
|
126
|
// ------------ Test Option Set Add ------------ //
|
127
|
// Load create form
|
128
|
$this->drupalGet('admin/config/media/flexslider/add');
|
129
|
$this->assertResponse(200, t('Administrative user can reach the "Add" form.'));
|
130
|
|
131
|
// Save new optionset
|
132
|
$optionset = array();
|
133
|
$optionset['title'] = 'testset';
|
134
|
$optionset['name'] = 'testset';
|
135
|
$this->drupalPost('admin/config/media/flexslider/add', $optionset, t('Save'));
|
136
|
$this->assertResponse(200);
|
137
|
$this->assertText('testset has been created.', t('Successfully saved the new optionset "testset"'));
|
138
|
|
139
|
// Attempt to save option set of the same name again
|
140
|
$this->drupalPost('admin/config/media/flexslider/add', $optionset, t('Save'));
|
141
|
$this->assertResponse(200);
|
142
|
$this->assertText("The machine-readable name is already in use. It must be unique.", t("Blocked the creation of duplicate named optionset."));
|
143
|
|
144
|
// ------------ Test Option Set Edit ------------ //
|
145
|
// Attempt to save each option value
|
146
|
$options = _flexslider_test_options();
|
147
|
|
148
|
foreach ($options['valid'] as $testset) {
|
149
|
$this->drupalPost('admin/config/media/flexslider/list/default/edit', $testset, t('Save'));
|
150
|
$this->assertResponse(200);
|
151
|
|
152
|
// Test saved values loaded into form
|
153
|
$this->drupalGet('admin/config/media/flexslider/list/default/edit');
|
154
|
$this->assertResponse(200, t('Default optionset reloaded.'));
|
155
|
foreach ($testset as $key => $option) {
|
156
|
$this->assertFieldByName($key, $option, t('Value for @key appears in form correctly.', array('@key' => $key)));
|
157
|
}
|
158
|
}
|
159
|
|
160
|
// ------------ Test Option Set Delete ------------ //
|
161
|
$testset = flexslider_optionset_load('testset');
|
162
|
|
163
|
// Test the delete workflow
|
164
|
$this->drupalGet("admin/config/media/flexslider/list/$testset->name/delete");
|
165
|
$this->assertResponse(200);
|
166
|
$this->assertText("Are you sure you want to delete $testset->name?", t('Delete confirmation form loaded.'));
|
167
|
$this->drupalPost("admin/config/media/flexslider/list/$testset->name/delete", '', 'Delete');
|
168
|
$this->assertResponse(200);
|
169
|
$this->assertText("The item has been deleted.", t('Deleted test set using form.'));
|
170
|
}
|
171
|
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Test configuration options
|
176
|
*
|
177
|
* @return array
|
178
|
* Returns an array of options to test saving.
|
179
|
*/
|
180
|
function _flexslider_test_options() {
|
181
|
// Valid option set data
|
182
|
$valid = array(
|
183
|
'set1' => _flexslider_optionset_defaults(),
|
184
|
'set2' => array(
|
185
|
'animation' => 'slide',
|
186
|
'startAt' => 4,
|
187
|
// @todo add more option tests
|
188
|
),
|
189
|
);
|
190
|
|
191
|
// Invalid edge cases
|
192
|
$error = array();
|
193
|
|
194
|
return array('valid' => $valid, 'error' => $error);
|
195
|
}
|