Skip to content

Commit 3047137

Browse files
Mile23torenware
Mile23
authored andcommitted
Issue #2821387 by Mile23, rfay: Convert all tests to BrowserTestBase
1 parent c4535ea commit 3047137

File tree

20 files changed

+488
-269
lines changed

20 files changed

+488
-269
lines changed

block_example/tests/src/Functional/BlockExampleMenuTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Drupal\Tests\BrowserTestBase;
66

7-
87
/**
98
* Test the user-facing menus in Block Example.
109
*
@@ -36,15 +35,15 @@ class BlockExampleMenuTest extends BrowserTestBase {
3635
*/
3736
public function testBlockExampleLink() {
3837
$this->drupalGet('');
39-
$this->assertSession()->linkByHrefExists('examples/block-example');
38+
$this->assertLinkByHref('examples/block-example');
4039
}
4140

4241
/**
4342
* Tests block_example menus.
4443
*/
4544
public function testBlockExampleMenu() {
4645
$this->drupalGet('examples/block-example');
47-
$this->assertSession()->statusCodeEquals(200);
46+
$this->assertResponse(200, 'Description page exists.');
4847
}
4948

5049
}

block_example/tests/src/Functional/BlockExampleTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public function testBlockExampleBasic() {
8181
// Verify that new content is shown.
8282
$this->drupalGet('');
8383
$assert->statusCodeEquals(200);
84-
8584
$assert->pageTextContains($edit['settings[block_example_string_text]']);
8685
}
8786

cache_example/src/Tests/CacheExampleTestCase.php cache_example/tests/src/Functional/CacheExampleTest.php

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Drupal\cache_example\Tests;
3+
namespace Drupal\Tests\cache_example\Functional;
44

5-
use Drupal\simpletest\WebTestBase;
5+
use Drupal\Tests\BrowserTestBase;
66

77
/**
88
* Tests for the cache_example module.
@@ -12,7 +12,7 @@
1212
* @group cache_example
1313
* @group examples
1414
*/
15-
class CacheExampleTestCase extends WebTestBase {
15+
class CacheExampleTest extends BrowserTestBase {
1616

1717
/**
1818
* Modules to enable.
@@ -37,14 +37,17 @@ class CacheExampleTestCase extends WebTestBase {
3737
*/
3838
public function testCacheExampleMenu() {
3939

40+
$assert = $this->assertSession();
41+
4042
// Test for a link to the cache_example in the Tools menu.
4143
$this->drupalGet('');
42-
$this->assertResponse(200, 'The Home page is available.');
43-
$this->assertLinkByHref('examples/cache-example');
44+
$assert->statusCodeEquals(200);
45+
46+
$assert->linkByHrefExists('examples/cache-example');
4447

4548
// Verify if the can successfully access the cache_example form.
4649
$this->drupalGet('examples/cache-example');
47-
$this->assertResponse(200, 'The Cache Example description page is available.');
50+
$assert->statusCodeEquals(200);
4851
}
4952

5053
/**
@@ -57,6 +60,7 @@ public function testCacheExampleMenu() {
5760
* - Clear cache at the end and test if displaying uncached version again.
5861
*/
5962
public function testCacheExampleBasic() {
63+
$assert = $this->assertSession();
6064

6165
// We need administrative privileges to clear the cache.
6266
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
@@ -65,42 +69,42 @@ public function testCacheExampleBasic() {
6569
// Get initial page cache example page, first time accessed,
6670
// and assert uncached output.
6771
$this->drupalGet('examples/cache-example');
68-
$this->assertText('Source: actual file search');
72+
$assert->pageTextContains('Source: actual file search');
6973

7074
// Reload the page; the number should be cached.
7175
$this->drupalGet('examples/cache-example');
72-
$this->assertText('Source: cached');
76+
$assert->pageTextContains('Source: cached');
7377

7478
// Now push the button to remove the count.
7579
$this->drupalPostForm('examples/cache-example', array(), t('Explicitly remove cached file count'));
76-
$this->assertText('Source: actual file search');
80+
$assert->pageTextContains('Source: actual file search');
7781

7882
// Create a cached item. First make sure it doesn't already exist.
79-
$this->assertText('Cache item does not exist');
83+
$assert->pageTextContains('Cache item does not exist');
8084
$this->drupalPostForm('examples/cache-example', array('expiration' => -10), t('Create a cache item with this expiration'));
8185
// We should now have an already-expired item. Automatically invalid.
82-
$this->assertText('Cache_item is invalid');
86+
$assert->pageTextContains('Cache_item is invalid');
8387
// Now do the expiration operation.
8488
$this->drupalPostForm('examples/cache-example', array('cache_clear_type' => 'expire'), t('Clear or expire cache'));
8589
// And verify that it was removed.
86-
$this->assertText('Cache item does not exist');
90+
$assert->pageTextContains('Cache item does not exist');
8791

8892
// Create a cached item. This time we'll make it not expire.
8993
$this->drupalPostForm('examples/cache-example', array('expiration' => 'never_remove'), t('Create a cache item with this expiration'));
9094
// We should now have an never-remove item.
91-
$this->assertText('Cache item exists and is set to expire at Never expires');
95+
$assert->pageTextContains('Cache item exists and is set to expire at Never expires');
9296
// Now do the expiration operation.
9397
$this->drupalPostForm('examples/cache-example', array('cache_clear_type' => 'expire'), t('Clear or expire cache'));
9498
// And verify that it was not removed.
95-
$this->assertText('Cache item exists and is set to expire at Never expires');
99+
$assert->pageTextContains('Cache item exists and is set to expire at Never expires');
96100
// Now do tag invalidation.
97101
$this->drupalPostForm('examples/cache-example', array('cache_clear_type' => 'remove_tag'), t('Clear or expire cache'));
98102
// And verify that it was invalidated.
99-
$this->assertText('Cache_item is invalid');
103+
$assert->pageTextContains('Cache_item is invalid');
100104
// Do the hard delete.
101105
$this->drupalPostForm('examples/cache-example', array('cache_clear_type' => 'remove_all'), t('Clear or expire cache'));
102106
// And verify that it was removed.
103-
$this->assertText('Cache item does not exist');
107+
$assert->pageTextContains('Cache item does not exist');
104108
}
105109

106110
}

config_entity_example/src/Tests/ConfigEntityExampleTest.php config_entity_example/tests/src/Functional/ConfigEntityExampleTest.php

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Drupal\config_entity_example\Tests;
4-
5-
use Drupal\simpletest\WebTestBase;
3+
namespace Drupal\Tests\config_entity_example\Functional;
4+
use Drupal\config_entity_example\Entity\Robot;
5+
use Drupal\Tests\BrowserTestBase;
66

77
/**
88
* Test the Config Entity Example module.
@@ -12,7 +12,7 @@
1212
*
1313
* @ingroup config_entity_example
1414
*/
15-
class ConfigEntityExampleTest extends WebTestBase {
15+
class ConfigEntityExampleTest extends BrowserTestBase {
1616

1717
/**
1818
* Modules to enable.
@@ -56,9 +56,11 @@ public static function getInfo() {
5656
* 5) Verify that the label is shown in the list.
5757
*/
5858
public function testConfigEntityExample() {
59+
$assert = $this->assertSession();
60+
5961
// 1) Verify that the Marvin entity was created when the module was
6062
// installed.
61-
$entity = entity_load('robot', 'marvin');
63+
$entity = Robot::load('marvin');
6264
$this->assertNotNull($entity, 'Marvin was created during installation.');
6365

6466
// 2) Verify that permissions are applied to the various defined paths.
@@ -74,7 +76,7 @@ public function testConfigEntityExample() {
7476
// we haven't logged in any users, so the client is anonymous.
7577
foreach ($forbidden_paths as $path) {
7678
$this->drupalGet($path);
77-
$this->assertResponse(403, "Access denied to anonymous for path: $path");
79+
$assert->statusCodeEquals(403);
7880
}
7981

8082
// Create a user with no permissions.
@@ -84,7 +86,7 @@ public function testConfigEntityExample() {
8486
// special permissions for these paths.
8587
foreach ($forbidden_paths as $path) {
8688
$this->drupalGet($path);
87-
$this->assertResponse(403, "Access denied to generic user for path: $path");
89+
$assert->statusCodeEquals(403);
8890
}
8991

9092
// Create a user who can administer robots.
@@ -93,12 +95,12 @@ public function testConfigEntityExample() {
9395
// Forbidden paths aren't forbidden any more.
9496
foreach ($forbidden_paths as $unforbidden) {
9597
$this->drupalGet($unforbidden);
96-
$this->assertResponse(200, "Access granted to admin user for path: $unforbidden");
98+
$assert->statusCodeEquals(200);
9799
}
98100

99101
// Now that we have the admin user logged in, check the menu links.
100102
$this->drupalGet('');
101-
$this->assertLinkByHref('examples/config-entity-example');
103+
$assert->linkByHrefExists('examples/config-entity-example');
102104

103105
// 3) Verify that we can manage entities through the user interface.
104106
// We still have the admin user logged in, so we'll create, update, and
@@ -119,8 +121,8 @@ public function testConfigEntityExample() {
119121

120122
// 4) Verify that our robot appears when we edit it.
121123
$this->drupalGet('/examples/config-entity-example/manage/' . $robot_machine_name);
122-
$this->assertField('label');
123-
$this->assertFieldChecked('edit-floopy');
124+
$assert->fieldExists('label');
125+
$assert->checkboxChecked('edit-floopy');
124126

125127
// 5) Verify that the label and machine name are shown in the list.
126128
$this->drupalGet('/examples/config-entity-example');
@@ -137,8 +139,8 @@ public function testConfigEntityExample() {
137139
t('Create Robot')
138140
);
139141
$this->drupalGet('/examples/config-entity-example');
140-
$this->assertText($robby_label);
141-
$this->assertText($robby_machine_name);
142+
$assert->pageTextContains($robby_label);
143+
$assert->pageTextContains($robby_machine_name);
142144

143145
// 6) Verify that required links are present on respective paths.
144146
$this->assertLinkByHref('/examples/config-entity-example/add');

content_entity_example/src/Tests/ContentEntityExampleTest.php content_entity_example/tests/src/Functional/ContentEntityExampleTest.php

+31-28
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Drupal\content_entity_example\Tests;
44

55
use Drupal\content_entity_example\Entity\Contact;
6-
use Drupal\examples\Tests\ExamplesTestBase;
6+
use Drupal\Tests\examples\Functional\ExamplesBrowserTestBase;
77

88
/**
99
* Tests the basic functions of the Content Entity Example module.
@@ -15,14 +15,16 @@
1515
* @group content_entity_example
1616
* @group examples
1717
*/
18-
class ContentEntityExampleTest extends ExamplesTestBase {
18+
class ContentEntityExampleTest extends ExamplesBrowserTestBase {
1919

2020
public static $modules = array('content_entity_example', 'block', 'field_ui');
2121

2222
/**
2323
* Basic tests for Content Entity Example.
2424
*/
2525
public function testContentEntityExample() {
26+
$assert = $this->assertSession();
27+
2628
$web_user = $this->drupalCreateUser(array(
2729
'add contact entity',
2830
'edit contact entity',
@@ -35,26 +37,26 @@ public function testContentEntityExample() {
3537
));
3638

3739
// Anonymous User should not see the link to the listing.
38-
$this->assertNoText(t('Content Entity Example: Contacts Listing'));
40+
$assert->pageTextNotContains('Content Entity Example: Contacts Listing');
3941

4042
$this->drupalLogin($web_user);
4143

4244
// Web_user user has the right to view listing.
43-
$this->assertLink(t('Content Entity Example: Contacts Listing'));
45+
$assert->linkExists('Content Entity Example: Contacts Listing');
4446

45-
$this->clickLink(t('Content Entity Example: Contacts Listing'));
47+
$this->clickLink('Content Entity Example: Contacts Listing');
4648

4749
// WebUser can add entity content.
48-
$this->assertLink(t('Add Contact'));
50+
$assert->linkExists('Add Contact');
4951

5052
$this->clickLink(t('Add Contact'));
5153

52-
$this->assertFieldByName('name[0][value]', '', 'Name Field, empty');
53-
$this->assertFieldByName('name[0][value]', '', 'First Name Field, empty');
54-
$this->assertFieldByName('name[0][value]', '', 'Gender Field, empty');
54+
$assert->fieldValueEquals('name[0][value]', '');
55+
$assert->fieldValueEquals('name[0][value]', '');
56+
$assert->fieldValueEquals('name[0][value]', '');
5557

5658
$user_ref = $web_user->name->value . ' (' . $web_user->id() . ')';
57-
$this->assertFieldByName('user_id[0][target_id]', $user_ref, 'User ID reference field points to web_user');
59+
$assert->fieldValueEquals('user_id[0][target_id]', $user_ref);
5860

5961
// Post content, save an instance. Go back to list after saving.
6062
$edit = array(
@@ -65,44 +67,46 @@ public function testContentEntityExample() {
6567
$this->drupalPostForm(NULL, $edit, t('Save'));
6668

6769
// Entity listed.
68-
$this->assertLink(t('Edit'));
69-
$this->assertLink(t('Delete'));
70+
$assert->linkExists('Edit');
71+
$assert->linkExists('Delete');
7072

7173
$this->clickLink('test name');
7274

7375
// Entity shown.
74-
$this->assertText(t('test name'));
75-
$this->assertText(t('test first name'));
76-
$this->assertText(t('male'));
77-
$this->assertLink(t('Add Contact'));
78-
$this->assertLink(t('Edit'));
79-
$this->assertLink(t('Delete'));
76+
$assert->pageTextContains('test name');
77+
$assert->pageTextContains('test first name');
78+
$assert->pageTextContains('male');
79+
$assert->linkExists('Add Contact');
80+
$assert->linkExists('Edit');
81+
$assert->linkExists('Delete');
8082

8183
// Delete the entity.
8284
$this->clickLink('Delete');
8385

8486
// Confirm deletion.
85-
$this->assertLink(t('Cancel'));
87+
$assert->linkExists('Cancel');
8688
$this->drupalPostForm(NULL, array(), 'Delete');
8789

8890
// Back to list, must be empty.
89-
$this->assertNoText('test name');
91+
$assert->pageTextNotContains('test name');
9092

9193
// Settings page.
9294
$this->drupalGet('admin/structure/content_entity_example_contact_settings');
93-
$this->assertText(t('Contact Settings'));
95+
$assert->pageTextContains('Contact Settings');
9496

9597
// Make sure the field manipulation links are available.
96-
$this->assertLink(t('Settings'));
97-
$this->assertLink(t('Manage fields'));
98-
$this->assertLink(t('Manage form display'));
99-
$this->assertLink(t('Manage display'));
98+
$assert->linkExists('Settings');
99+
$assert->linkExists('Manage fields');
100+
$assert->linkExists('Manage form display');
101+
$assert->linkExists('Manage display');
100102
}
101103

102104
/**
103105
* Test all paths exposed by the module, by permission.
104106
*/
105107
public function testPaths() {
108+
$assert = $this->assertSession();
109+
106110
// Generate a contact so that we can test the paths against it.
107111
$contact = Contact::create(
108112
array(
@@ -129,7 +133,7 @@ public function testPaths() {
129133
$this->drupalLogin($user);
130134
}
131135
$this->drupalGet($datum[1]);
132-
$this->assertResponse($datum[0]);
136+
$assert->statusCodeEquals($datum[0]);
133137
}
134138
}
135139

@@ -237,8 +241,7 @@ public function testAddFields() {
237241

238242
// Fetch url without query parameters.
239243
$current_path = strtok($this->getUrl(), '?');
240-
$this->assertEqual($expected_path, $current_path, 'It should redirect to field storage settings page.');
241-
244+
$this->assertEquals($expected_path, $current_path);
242245
}
243246

244247
}

0 commit comments

Comments
 (0)