ConfigEntityExampleTest.php
4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* @file
* Test case for testing the Config Entity Example module.
*/
namespace Drupal\config_entity_example\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Test the Config Entity Example module.
*
* @group config_entity_example
* @group examples
*
* @ingroup config_entity_example
*/
class ConfigEntityExampleTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('config_entity_example');
/**
* The installation profile to use with this test.
*
* We need the 'minimal' profile in order to make sure the Tool block is
* available.
*
* @var string
*/
protected $profile = 'minimal';
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Config Entity Example functional test',
'description' => 'Test the Config Entity Example module.',
'group' => 'Examples',
);
}
/**
* Various functional test of the Config Entity Example module.
*
* 1) Verify that the Marvin entity was created when the module was installed.
*
* 2) Verify that permissions are applied to the various defined paths.
*
* 3) Verify that we can manage entities through the user interface.
*
* 4) Verify that the entity we add can be re-edited.
*
* 5) Verify that the label is shown in the list.
*/
public function testConfigEntityExample() {
// 1) Verify that the Marvin entity was created when the module was
// installed.
$entity = entity_load('robot', 'marvin');
$this->assertNotNull($entity, 'Marvin was created during installation.');
// 2) Verify that permissions are applied to the various defined paths.
// Define some paths. Since the Marvin entity is defined, we can use it
// in our management paths.
$forbidden_paths = array(
'/examples/config_entity_example',
'/examples/config_entity_example/add',
'/examples/config_entity_example/manage/marvin',
'/examples/config_entity_example/manage/marvin/delete',
);
// Check each of the paths to make sure we don't have access. At this point
// we haven't logged in any users, so the client is anonymous.
foreach ($forbidden_paths as $path) {
$this->drupalGet($path);
$this->assertResponse(403, "Access denied to anonymous for path: $path");
}
// Create a user with no permissions.
$noperms_user = $this->drupalCreateUser();
$this->drupalLogin($noperms_user);
// Should be the same result for forbidden paths, since the user needs
// special permissions for these paths.
foreach ($forbidden_paths as $path) {
$this->drupalGet($path);
$this->assertResponse(403, "Access denied to generic user for path: $path");
}
// Create a user who can administer robots.
$admin_user = $this->drupalCreateUser(array('administer robots'));
$this->drupalLogin($admin_user);
// Forbidden paths aren't forbidden any more.
foreach ($forbidden_paths as $unforbidden) {
$this->drupalGet($unforbidden);
$this->assertResponse(200, "Access granted to admin user for path: $unforbidden");
}
// Now that we have the admin user logged in, check the menu links.
$this->drupalGet('');
$this->assertLinkByHref('examples/config_entity_example');
// 3) Verify that we can manage entities through the user interface.
// We still have the admin user logged in, so we'll create, update, and
// delete an entity.
// Go to the list page.
$this->drupalGet('/examples/config_entity_example');
$this->clickLink('Add robot');
$robot_machine_name = 'roboname';
$this->drupalPostForm(
NULL,
array(
'label' => $robot_machine_name,
'id' => $robot_machine_name,
'floopy' => TRUE,
),
t('Create Robot')
);
// 4) Verify that our robot appears when we edit it.
$this->drupalGet('/examples/config_entity_example/manage/' . $robot_machine_name);
$this->assertField('label');
$this->assertFieldChecked('edit-floopy');
// 5) Verify that the label and machine name are shown in the list.
$this->drupalGet('/examples/config_entity_example');
$this->clickLink('Add robot');
$robby_machine_name = 'robby_machine_name';
$robby_label = 'Robby label';
$this->drupalPostForm(
NULL,
array(
'label' => $robby_label,
'id' => $robby_machine_name,
'floopy' => TRUE,
),
t('Create Robot')
);
$this->drupalGet('/examples/config_entity_example');
$this->assertText($robby_label);
$this->assertText($robby_machine_name);
}
}