Commit ece1765f authored by fenstrat's avatar fenstrat Committed by fenstrat

Issue #2968014 by fenstrat: Convert WTB tests to BTB (and fix failing tests)

parent 91d73306
......@@ -17,6 +17,7 @@ content:
weight: 10
settings: { }
third_party_settings: { }
region: content
field_block_field_test:
weight: 32
settings:
......@@ -24,23 +25,34 @@ content:
plugin_id: ''
third_party_settings: { }
type: block_field_default
region: content
path:
type: path
weight: 30
settings: { }
third_party_settings: { }
region: content
promote:
type: boolean_checkbox
settings:
display_label: true
weight: 15
third_party_settings: { }
region: content
status:
type: boolean_checkbox
settings:
display_label: true
weight: 120
region: content
third_party_settings: { }
sticky:
type: boolean_checkbox
settings:
display_label: true
weight: 16
third_party_settings: { }
region: content
title:
type: string_textfield
weight: -5
......@@ -48,6 +60,7 @@ content:
size: 60
placeholder: ''
third_party_settings: { }
region: content
uid:
type: entity_reference_autocomplete
weight: 5
......@@ -56,4 +69,5 @@ content:
size: 60
placeholder: ''
third_party_settings: { }
region: content
hidden: { }
......@@ -18,8 +18,10 @@ content:
settings: { }
third_party_settings: { }
type: block_field
region: content
links:
weight: 100
settings: { }
third_party_settings: { }
region: content
hidden: { }
......@@ -14,5 +14,6 @@ mode: teaser
content:
links:
weight: 100
region: content
hidden:
field_block_field_test: true
......@@ -2,14 +2,17 @@
namespace Drupal\block_field\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;
/**
* Tests block field widgets and formatters.
*
* @group block_field
*/
class BlockFieldTest extends WebTestBase {
class BlockFieldTest extends BrowserTestBase {
use StringTranslationTrait;
/**
* Modules to enable.
......@@ -29,6 +32,8 @@ class BlockFieldTest extends WebTestBase {
* Tests block field.
*/
public function testBlockField() {
$assert = $this->assertSession();
$admin_user = $this->drupalCreateUser([
'access content',
'administer nodes',
......@@ -42,30 +47,32 @@ class BlockFieldTest extends WebTestBase {
// Check that add more and ajax callbacks are working as expected.
$this->drupalPostForm('node/add/block_field_test', [
'title[0][value]' => 'Block field test',
], t('Add another item'));
$this->drupalPostForm(NULL, [], t('Add another item'));
], $this->t('Add another item'));
$this->drupalPostForm(NULL, [], $this->t('Add another item'));
$this->drupalPostForm(NULL, [
'field_block_field_test[0][plugin_id]' => 'block_field_test_authenticated',
'field_block_field_test[1][plugin_id]' => 'block_field_test_content',
'field_block_field_test[2][plugin_id]' => 'block_field_test_time',
], t('Add another item'));
], $this->t('Add another item'));
$this->drupalPostForm(NULL, [
'field_block_field_test[0][plugin_id]' => 'block_field_test_authenticated',
'field_block_field_test[1][plugin_id]' => 'block_field_test_content',
'field_block_field_test[2][plugin_id]' => 'block_field_test_time',
], t('Add another item'));
$this->drupalPostForm(NULL, [], t('Save and publish'));
], $this->t('Add another item'));
$this->drupalPostForm(NULL, [], $this->t('Save'));
// Check blocks displayed to authenticated.
$this->drupalGet('node/1');
$this->assertRaw('<div class="field field--name-field-block-field-test field--type-block-field field--label-above">');
$this->assertRaw('<div class="field__label">Block field test</div>');
$this->assertRaw('<h2>You are logged in as...</h2>');
$this->assertRaw('<p><span>' . $admin_user->label() . '</span></p>');
$this->assertRaw('<h2>Block field test content</h2>');
$this->assertRaw('This block was created at');
$this->assertRaw('<h2>The time is...</h2>');
$this->assertPattern('/\d\d:\d\d:\d\d/');
$node = $this->drupalGetNodeByTitle('Block field test');
$this->drupalGet($node->toUrl());
$selector = '.field--name-field-block-field-test';
$assert->elementExists('css', $selector);
$assert->elementContains('css', $selector, '<div class="field__label">Block field test</div>');
$assert->elementContains('css', $selector, '<h2>You are logged in as...</h2>');
$assert->elementTextContains('css', $selector, $admin_user->label());
$assert->elementContains('css', $selector, '<h2>Block field test content</h2>');
$assert->elementTextContains('css', $selector, 'This block was created at');
$assert->elementContains('css', $selector, '<h2>The time is...</h2>');
$assert->responseMatches('/\d\d:\d\d:\d\d/');
// Create a block_field_test node.
$block_node = $this->drupalCreateNode([
......@@ -79,23 +86,22 @@ class BlockFieldTest extends WebTestBase {
'label_display' => TRUE,
];
$block_node->save();
$this->drupalGet('node/' . $block_node->id());
$this->assertRaw('<h2>Authenticated</h2>');
$this->assertRaw('<p><span>' . $admin_user->label() . '</span></p>');
$this->drupalGet($block_node->toUrl());
$assert->elementContains('css', $selector, '<h2>Authenticated</h2>');
$assert->elementTextContains('css', $selector, $admin_user->label());
// Check block_field_test_authenticated cache dependency is respected when
// the user's name is updated.
$admin_user->setUsername('admin_user');
$admin_user->save();
$this->drupalGet('node/' . $block_node->id());
$this->assertRaw('<h2>Authenticated</h2>');
$this->assertRaw('<p><span>admin_user</span></p>');
$this->drupalGet($block_node->toUrl());
$assert->elementContains('css', $selector, '<h2>Authenticated</h2>');
$assert->elementTextContains('css', $selector, 'admin_user');
// Check authenticated block is not visible to anonymous users.
$this->drupalLogout();
$this->drupalGet('node/' . $block_node->id());
$this->assertNoRaw('<h2>Authenticated</h2>');
$this->assertNoRaw('<p><span>' . $admin_user->label() . '</span></p>');
$this->drupalGet($block_node->toUrl());
$assert->elementNotExists('css', $selector);
// Check content block.
$block_node->field_block_field_test->plugin_id = 'block_field_test_content';
......@@ -106,9 +112,9 @@ class BlockFieldTest extends WebTestBase {
];
$block_node->save();
$this->drupalGet('node/' . $block_node->id());
$this->assertRaw('<h2>Hello</h2>');
$this->assertRaw('<p>World</p>');
$this->drupalGet($block_node->toUrl());
$assert->elementContains('css', $selector, '<h2>Hello</h2>');
$assert->elementContains('css', $selector, '<p>World</p>');
// ISSUE: Drupal's page cache it not respecting the time block max age,
// so we need to log in to bypass page caching.
......@@ -123,27 +129,27 @@ class BlockFieldTest extends WebTestBase {
$block_node->save();
// Check that time is set.
$this->drupalGet('node/' . $block_node->id());
$this->assertPattern('/\d\d:\d\d:\d\d \(\d+\)/');
$this->drupalGet($block_node->toUrl());
$assert->responseMatches('/\d\d:\d\d:\d\d \(\d+\)/');
// Get the current time.
preg_match('/\d\d:\d\d:\d\d \(\d+\)/', $this->getRawContent(), $match);
preg_match('/\d\d:\d\d:\d\d \(\d+\)/', $this->getSession()->getPage()->getContent(), $match);
$time = $match[0];
$this->assertRaw($time);
$assert->responseContains($time);
// Have delay test one second so that the time is updated.
sleep(1);
// Check that time is never cached by reloading the page.
$this->drupalGet('node/' . $block_node->id());
$this->assertPattern('/\d\d:\d\d:\d\d \(\d+\)/');
$this->assertNoRaw($time);
$this->drupalGet($block_node->toUrl());
$assert->responseMatches('/\d\d:\d\d:\d\d \(\d+\)/');
$assert->responseNotContains($time);
$this->drupalGet('admin/structure/types/manage/block_field_test/fields/node.block_field_test.field_block_field_test');
$this->drupalPostForm(NULL, ['settings[plugin_ids][page_title_block]' => FALSE], t('Save settings'));
$this->drupalPostForm(NULL, ['settings[plugin_ids][page_title_block]' => FALSE], $this->t('Save settings'));
$this->drupalGet('admin/structure/types/manage/block_field_test/fields/node.block_field_test.field_block_field_test');
$this->assertResponse(200);
$assert->statusCodeEquals(200);
}
}
......@@ -18,24 +18,27 @@ class WidgetTest extends BrowserTestBase {
* {@inheritdoc}
*/
public static $modules = [
'node',
'user',
'block',
'block_field',
'block_field_widget_test',
'field_ui',
];
/**
* The admin user.
*
* @var \Drupal\user\UserInterface
*/
private $adminUser;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->adminUser = $this->createAdminUser();
$this->drupalLogin($this->adminUser);
$this->drupalLogin($this->drupalCreateUser([
'access content',
'administer nodes',
'administer content types',
'bypass node access',
'administer node fields',
]));
}
/**
......@@ -60,16 +63,6 @@ class WidgetTest extends BrowserTestBase {
}
}
/**
* Create an admin user.
*
* @return \Drupal\user\UserInterface
* A user with all permissions.
*/
private function createAdminUser() {
return $this->drupalCreateUser(array_keys($this->container->get('user.permissions')->getPermissions()));
}
/**
* Create dummy nodes.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment