View source
<?php
namespace Drupal\Tests\flag\Functional;
use Drupal\Core\Url;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
class LinkTypeFieldEntryTest extends FlagTestBase {
use FieldUiTestTrait;
protected $nodeId;
protected $flagConfirmMessage = 'Flag test label 123?';
protected $flagDetailsMessage = 'Enter flag test label 123 details';
protected $unflagConfirmMessage = 'Unflag test label 123?';
protected $createButtonText = 'Create flagging 123?';
protected $deleteButtonText = 'Delete flagging 123?';
protected $updateButtonText = 'Updating flagging 123?';
protected $flagFieldId = 'flag_text_field';
protected $flagFieldLabel = 'Flag Text Field';
protected $flagFieldValue;
protected $flag;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
}
public function testCreateFieldEntryFlag() {
$this
->drupalLogin($this->adminUser);
$this
->doCreateFlag();
$this
->doAddFields();
$this
->doFlagNode();
$this
->doEditFlagField();
$this
->doBadEditFlagField();
$this
->doUnflagNode();
}
public function doFlagUIfieldPlugin() {
$this
->drupalPostForm('admin/structure/flags/add', [], t('Continue'));
$edit = [
'link_type' => 'field_entry',
];
$this
->drupalPostAjaxForm(NULL, $edit, 'link_type');
$this
->assertText(t('Flag confirmation message'));
$this
->assertText(t('Enter flagging details message'));
$this
->assertText(t('Unflag confirmation message'));
}
public function doCreateFlag() {
$edit = [
'bundles' => [
$this->nodeType,
],
'linkTypeConfig' => [
'flag_confirmation' => $this->flagConfirmMessage,
'unflag_confirmation' => $this->unflagConfirmMessage,
'edit_flagging' => $this->flagDetailsMessage,
'flag_create_button' => $this->createButtonText,
'flag_delete_button' => $this->deleteButtonText,
'flag_update_button' => $this->updateButtonText,
],
'link_type' => 'field_entry',
];
$this->flag = $this
->createFlagFromArray($edit);
}
public function doAddFields() {
$flag_id = $this->flag
->id();
$this
->drupalGet('admin/structure/flags/manage/' . $flag_id);
$this
->assertText(t("Manage fields"), "The Field UI tabs appear on the flag edit form page.");
$this
->fieldUIAddNewField('admin/structure/flags/manage/' . $flag_id, $this->flagFieldId, $this->flagFieldLabel, 'text');
}
public function doFlagNode() {
$node = $this
->drupalCreateNode([
'type' => $this->nodeType,
]);
$this->nodeId = $node
->id();
$this
->grantFlagPermissions($this->flag);
$user_1 = $this
->drupalCreateUser();
$this
->drupalLogin($user_1);
$this
->drupalGet('node/' . $this->nodeId);
$this
->clickLink($this->flag
->getShortText('flag'));
$this
->assertText($this->flagConfirmMessage);
$this->flagFieldValue = $this
->randomString();
$edit = [
'field_' . $this->flagFieldId . '[0][value]' => $this->flagFieldValue,
];
$this
->drupalPostForm(NULL, $edit, $this->createButtonText);
$this
->assertLink($this->flag
->getShortText('unflag'));
}
public function doEditFlagField() {
$flag_id = $this->flag
->id();
$this
->drupalGet('node/' . $this->nodeId);
$this
->clickLink($this->flag
->getShortText('unflag'));
$node_url = Url::fromRoute('entity.node.canonical', [
'node' => $this->nodeId,
]);
$this
->assertUrl('flag/details/edit/' . $flag_id . '/' . $this->nodeId, [
'query' => [
'destination' => $node_url
->toString(),
],
]);
$this
->assertText($this->flagDetailsMessage);
$this
->assertFieldByName('field_' . $this->flagFieldId . '[0][value]', $this->flagFieldValue);
$this->flagFieldValue = $this
->randomString();
$edit = [
'field_' . $this->flagFieldId . '[0][value]' => $this->flagFieldValue,
];
$this
->drupalPostForm(NULL, $edit, $this->updateButtonText);
$this
->drupalGet('flag/details/edit/' . $flag_id . '/' . $this->nodeId);
$this
->assertFieldByName('field_' . $this->flagFieldId . '[0][value]', $this->flagFieldValue);
}
public function doBadEditFlagField() {
$flag_id = $this->flag
->id();
$this
->drupalGet('flag/details/edit/' . $flag_id . '/-9999');
$this
->assertResponse('404', 'Editing an invalid flagging path: good flag, bad entity.');
$this
->drupalGet('flag/details/edit/jibberish/' . $this->nodeId);
$this
->assertResponse('404', 'Editing an invalid flagging path: bad flag, good entity');
$unlinked_node = $this
->drupalCreateNode([
'type' => $this->nodeType,
]);
$this
->drupalGet('flag/details/edit/' . $flag_id . '/' . $unlinked_node
->id());
$this
->assertResponse('404', 'Editing an invalid flagging path: good flag, good entity, but not flagged');
}
public function doUnflagNode() {
$this
->drupalGet('node/' . $this->nodeId);
$this
->clickLink($this->flag
->getShortText('unflag'));
$this
->clickLink($this->deleteButtonText);
$this
->assertText($this->unflagConfirmMessage);
$this
->drupalPostForm(NULL, [], $this->deleteButtonText);
$this
->assertResponse(200);
$this
->drupalGet('node/' . $this->nodeId);
$this
->assertLink($this->flag
->getShortText('flag'));
}
}