View source
<?php
namespace Drupal\Tests\field_ui\Functional;
use Drupal\Core\Entity\Entity\EntityFormMode;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class EntityDisplayModeTest extends BrowserTestBase {
protected static $modules = [
'block',
'entity_test',
'field_ui',
'node',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('page_title_block');
}
public function testEntityViewModeUI() {
$this
->drupalGet('admin/structure/display-modes/view');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this
->drupalCreateUser([
'administer display modes',
]));
$this
->drupalGet('admin/structure/display-modes/view');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Add view mode');
$this
->assertSession()
->linkByHrefExists('admin/structure/display-modes/view/add');
$this
->assertSession()
->linkByHrefExists('admin/structure/display-modes/view/add/entity_test');
$this
->drupalGet('admin/structure/display-modes/view/add/entity_test_mulrev');
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalGet('admin/structure/display-modes/view/add');
$this
->assertSession()
->linkNotExists('Test entity - revisions and data table', 'An entity type with no view builder cannot have view modes.');
$this
->clickLink('Test entity');
$edit = [
'id' => strtolower($this
->randomMachineName()) . '.' . strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('The machine-readable name must contain only lowercase letters, numbers, and underscores.');
$edit = [
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("Saved the {$edit['label']} view mode.");
$this
->drupalGet('admin/structure/display-modes/view/manage/entity_test.' . $edit['id']);
$view_mode = EntityViewMode::load('entity_test.' . $edit['id']);
$this
->assertEquals(Url::fromRoute('entity.entity_view_mode.collection')
->toString(), $view_mode
->toUrl('collection')
->toString());
$this
->assertEquals(Url::fromRoute('entity.entity_view_mode.add_form', [
'entity_type_id' => $view_mode
->getTargetType(),
])
->toString(), $view_mode
->toUrl('add-form')
->toString());
$this
->assertEquals(Url::fromRoute('entity.entity_view_mode.edit_form', [
'entity_view_mode' => $view_mode
->id(),
])
->toString(), $view_mode
->toUrl('edit-form')
->toString());
$this
->assertEquals(Url::fromRoute('entity.entity_view_mode.delete_form', [
'entity_view_mode' => $view_mode
->id(),
])
->toString(), $view_mode
->toUrl('delete-form')
->toString());
$this
->clickLink('Delete');
$this
->assertSession()
->pageTextContains("Are you sure you want to delete the view mode {$edit['label']}?");
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains("The view mode {$edit['label']} has been deleted.");
}
public function testEntityFormModeUI() {
$this
->drupalGet('admin/structure/display-modes/form');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this
->drupalCreateUser([
'administer display modes',
]));
$this
->drupalGet('admin/structure/display-modes/form');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Add form mode');
$this
->assertSession()
->linkByHrefExists('admin/structure/display-modes/form/add');
$this
->drupalGet('admin/structure/display-modes/form/add/entity_test_no_label');
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalGet('admin/structure/display-modes/form/add');
$this
->assertSession()
->linkNotExists('Entity Test without label', 'An entity type with no form cannot have form modes.');
$this
->clickLink('Test entity');
$edit = [
'id' => strtolower($this
->randomMachineName()) . '.' . strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('The machine-readable name must contain only lowercase letters, numbers, and underscores.');
$edit = [
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("Saved the {$edit['label']} form mode.");
$this
->drupalGet('admin/structure/display-modes/form/manage/entity_test.' . $edit['id']);
$form_mode = EntityFormMode::load('entity_test.' . $edit['id']);
$this
->assertEquals(Url::fromRoute('entity.entity_form_mode.collection')
->toString(), $form_mode
->toUrl('collection')
->toString());
$this
->assertEquals(Url::fromRoute('entity.entity_form_mode.add_form', [
'entity_type_id' => $form_mode
->getTargetType(),
])
->toString(), $form_mode
->toUrl('add-form')
->toString());
$this
->assertEquals(Url::fromRoute('entity.entity_form_mode.edit_form', [
'entity_form_mode' => $form_mode
->id(),
])
->toString(), $form_mode
->toUrl('edit-form')
->toString());
$this
->assertEquals(Url::fromRoute('entity.entity_form_mode.delete_form', [
'entity_form_mode' => $form_mode
->id(),
])
->toString(), $form_mode
->toUrl('delete-form')
->toString());
$this
->clickLink('Delete');
$this
->assertSession()
->pageTextContains("Are you sure you want to delete the form mode {$edit['label']}?");
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains("The form mode {$edit['label']} has been deleted.");
}
public function testAlphabeticalDisplaySettings() {
$this
->drupalLogin($this
->drupalCreateUser([
'access administration pages',
'administer content types',
'administer display modes',
'administer nodes',
'administer node fields',
'administer node display',
'administer node form display',
'view the administration theme',
]));
$this
->drupalGet('admin/structure/types/manage/article/display');
$page_text = $this
->getTextContent();
$start = strpos($page_text, 'view modes');
$pos = $start;
$list = [
'Full content',
'RSS',
'Search index',
'Search result',
'Teaser',
];
foreach ($list as $name) {
$new_pos = strpos($page_text, $name, $start);
$this
->assertGreaterThan($pos, $new_pos);
$pos = $new_pos;
}
$edit = [
'label' => 'Breezer',
];
$this
->drupalGet('admin/structure/display-modes/view/manage/node.teaser');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Saved the Breezer view mode.');
$this
->drupalGet('admin/structure/types/manage/article/display');
$page_text = $this
->getTextContent();
$start = strpos($page_text, 'view modes');
$pos = $start;
$list = [
'Breezer',
'Full content',
];
foreach ($list as $name) {
$new_pos = strpos($page_text, $name, $start);
$this
->assertGreaterThan($pos, $new_pos);
$pos = $new_pos;
}
}
}