View source
<?php
namespace Drupal\Tests\flag\Functional;
class AdminUITest extends FlagTestBase {
protected $entityTypeManager;
protected $label = 'Test label 123';
protected $flagId = 'test_label_123';
protected $flag;
protected $node;
protected $nodeId;
protected $flagShortText = 'Flag this stuff';
protected $unflagShortText = 'Unflag this stuff';
protected function setUp() {
parent::setUp();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this
->drupalLogin($this->adminUser);
$this->node = $this
->drupalCreateNode([
'type' => $this->nodeType,
]);
$this->nodeId = $this->node
->id();
}
public function testFlagAdmin() {
$this
->doFlagAdd();
$this
->doFlagEdit();
$this
->doFlagDisable();
$this
->doFlagEnable();
$this
->doFlagReset();
$this
->doFlagChangeWeights();
$this
->doFlagDelete();
}
public function doFlagAdd() {
$this
->drupalPostForm('admin/structure/flags/add', [], $this
->t('Continue'));
$this
->assertText(t('Messages'));
$this
->assertText(t('Flag access'));
$this
->assertText(t('Display options'));
$edit = [
'label' => $this->label,
'id' => $this->flagId,
'bundles[' . $this->nodeType . ']' => $this->nodeType,
'flag_short' => $this->flagShortText,
'unflag_short' => $this->unflagShortText,
];
$this
->drupalPostForm(NULL, $edit, $this
->t('Create Flag'));
$this
->assertText(t('Flag @this_label has been added.', [
'@this_label' => $this->label,
]));
$this->flag = $this->flagService
->getFlagById($this->flagId);
$this
->assertNotNull($this->flag, 'The flag was created.');
$this
->grantFlagPermissions($this->flag);
}
public function doFlagEdit() {
$this
->drupalGet('admin/structure/flags/manage/' . $this->flagId);
$this
->assertSession()
->elementAttributeExists('css', '#edit-global-0', 'disabled');
}
public function doFlagDisable() {
$this
->drupalGet('admin/structure/flags');
$this
->assertText(t('Enabled'));
$this
->drupalPostForm('admin/structure/flags/manage/' . $this->flagId . '/disable', [], $this
->t('Disable'));
$this
->assertResponse(200);
$this
->drupalGet('admin/structure/flags');
$this
->assertText(t('Disabled'));
$this
->drupalGet('node/' . $this->nodeId);
$this
->assertNoText($this->flagShortText);
}
public function doFlagEnable() {
$this
->drupalGet('admin/structure/flags');
$this
->assertText(t('Disabled'));
$this
->drupalPostForm('admin/structure/flags/manage/' . $this->flagId . '/enable', [], $this
->t('Enable'));
$this
->assertResponse(200);
$this
->drupalGet('admin/structure/flags');
$this
->assertText(t('Enabled'));
$this
->drupalGet('node/' . $this->nodeId);
$this
->assertText($this->flagShortText);
}
public function doFlagReset() {
$this->flagService
->flag($this->flag, $this->node, $this->adminUser);
$query_before = $this->entityTypeManager
->getStorage('flagging')
->getQuery();
$query_before
->condition('flag_id', $this->flag
->id())
->condition('entity_type', 'node')
->condition('entity_id', $this->node
->id());
$ids_before = $query_before
->execute();
$this
->assertEqual(count($ids_before), 1, "The flag has one flagging.");
$this
->drupalGet('admin/structure/flags/manage/' . $this->flag
->id() . '/reset');
$this
->assertText($this
->t('Are you sure you want to reset the Flag'));
$this
->drupalPostForm(NULL, [], $this
->t('Reset'));
$query_after = $this->entityTypeManager
->getStorage('flagging')
->getQuery();
$query_after
->condition('flag_id', $this->flag
->id())
->condition('entity_type', 'node')
->condition('entity_id', $this->node
->id());
$ids_after = $query_after
->execute();
$this
->assertEqual(count($ids_after), 0, "The flag has no flaggings after being reset.");
}
public function doFlagChangeWeights() {
$flag_weights_to_set = [];
$flag_weights_to_set[$this->flagId] = 0;
foreach (range(1, 10) as $i) {
$flag = $this
->createFlag();
$flag_weights_to_set[$flag
->id()] = -$i;
}
$edit = [];
foreach ($flag_weights_to_set as $id => $weight) {
$edit['flags[' . $id . '][weight]'] = $weight;
}
$this
->drupalPostForm('admin/structure/flags', $edit, $this
->t('Save'));
$all_flags = $this->container
->get('entity_type.manager')
->getStorage('flag')
->loadMultiple();
foreach ($all_flags as $id => $flag) {
$this
->assertEqual($all_flags[$id]
->get('weight'), $flag_weights_to_set[$id], 'The flag weight was changed.');
}
}
public function doFlagDelete() {
$this
->drupalGet('node/' . $this->nodeId);
$this
->assertLink($this->flagShortText);
$this
->drupalGet('admin/structure/flags/manage/' . $this->flag
->id() . '/delete');
$this
->assertText($this
->t('Are you sure you want to delete the flag @this_label?', [
'@this_label' => $this->label,
]));
$this
->drupalPostForm(NULL, [], $this
->t('Delete'));
$result = $this->flagService
->getFlagById($this->flagId);
$this
->assertNull($result, 'The flag was deleted.');
$this
->drupalGet('node/' . $this->nodeId);
$this
->assertText($this->node
->label());
$this
->assertNoLink($this->flagShortText);
}
}