View source
<?php
namespace Drupal\Tests\flag\Kernel;
use Drupal\node\Entity\Node;
use Drupal\flag\Entity\Flag;
class AccessTest extends FlagKernelTestBase {
protected function setUp() {
parent::setUp();
$this
->installSchema('user', 'users_data', 'flag');
$this
->createUser();
}
public function testDefault() {
$flag = Flag::create([
'id' => 'example',
'label' => $this
->randomString(),
'entity_type' => 'node',
'bundles' => [
'article',
],
'flag_type' => 'entity:node',
'link_type' => 'reload',
'flagTypeConfig' => [],
'linkTypeConfig' => [],
]);
$flag
->save();
$user_alice = $this
->createUser([
'administer flags',
'flag example',
'unflag example',
]);
$user_jill = $this
->createUser([
'administer flags',
'flag example',
]);
$user_bob = $this
->createUser();
$article = Node::create([
'type' => 'article',
'title' => 'Article node',
]);
$article
->save();
$this
->assertTrue($flag
->actionAccess('flag', $user_alice, $article)
->isAllowed(), 'Alice can flag.');
$this
->assertTrue($flag
->actionAccess('unflag', $user_alice, $article)
->isAllowed(), 'Alice can unflag.');
$this
->assertTrue($flag
->actionAccess('flag', $user_jill, $article)
->isAllowed(), 'Jill can flag.');
$this
->assertTrue($flag
->actionAccess('unflag', $user_jill, $article)
->isNeutral(), 'Jill cannot unflag.');
$this
->assertTrue($flag
->actionAccess('flag', $user_bob, $article)
->isNeutral(), 'Bob cannot flag.');
$this
->assertTrue($flag
->actionAccess('unflag', $user_bob, $article)
->isNeutral(), 'Bob cannot unflag..');
}
public function testOwnersAccess() {
$flag = Flag::create([
'id' => 'me_myself_and_I',
'label' => 'Self Review Flag',
'entity_type' => 'node',
'bundles' => [
'article',
],
'flag_type' => 'entity:node',
'link_type' => 'reload',
'flagTypeConfig' => [
'extra_permissions' => [
'owner',
],
],
'linkTypeConfig' => [],
]);
$flag
->save();
$flag_id = $flag
->id();
$user_author = $this
->createUser([
"flag {$flag_id} own items",
"unflag {$flag_id} own items",
]);
$user_editor = $this
->createUser([
"flag {$flag_id} other items",
"unflag {$flag_id} other items",
]);
$article_by_author = Node::create([
'type' => 'article',
'title' => 'Article node',
]);
$article_by_author
->setOwner($user_author);
$article_by_author
->save();
$article_by_editor = Node::create([
'type' => 'article',
'title' => 'Article node',
]);
$article_by_editor
->setOwner($user_editor);
$article_by_editor
->save();
$this
->assertTrue($flag
->actionAccess('flag', $user_author, $article_by_author)
->isAllowed());
$this
->assertTrue($flag
->actionAccess('unflag', $user_author, $article_by_author)
->isAllowed());
$this
->assertTrue($flag
->actionAccess('flag', $user_author, $article_by_editor)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('unflag', $user_author, $article_by_editor)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('flag', $user_editor, $article_by_author)
->isAllowed());
$this
->assertTrue($flag
->actionAccess('unflag', $user_editor, $article_by_author)
->isAllowed());
$this
->assertTrue($flag
->actionAccess('flag', $user_editor, $article_by_editor)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('unflag', $user_editor, $article_by_editor)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('flag', $user_author)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('flag', $user_editor)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('unflag', $user_author)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('unflag', $user_editor)
->isNeutral());
}
public function testUserFlag() {
$flag = Flag::create([
'id' => 'A flag about users',
'label' => $this
->randomString(),
'entity_type' => 'user',
'flag_type' => 'entity:user',
'link_type' => 'reload',
'flagTypeConfig' => [
'extra_permissions' => [
'owner',
],
],
'linkTypeConfig' => [],
]);
$flag
->save();
$flag_id = $flag
->id();
$user_alice = $this
->createUser([
"flag {$flag_id} own user account",
"unflag {$flag_id} own user account",
]);
$user_bob = $this
->createUser([
"flag {$flag_id} other user accounts",
"unflag {$flag_id} other user accounts",
]);
$this
->assertTrue($flag
->actionAccess('flag', $user_alice, $user_alice)
->isAllowed());
$this
->assertTrue($flag
->actionAccess('unflag', $user_alice, $user_alice)
->isAllowed());
$this
->assertTrue($flag
->actionAccess('flag', $user_bob, $user_bob)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('unflag', $user_bob, $user_bob)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('flag', $user_alice, $user_bob)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('flag', $user_alice, $user_bob)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('unflag', $user_bob, $user_alice)
->isAllowed());
$this
->assertTrue($flag
->actionAccess('unflag', $user_bob, $user_alice)
->isAllowed());
$this
->assertTrue($flag
->actionAccess('flag', $user_alice)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('flag', $user_bob)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('unflag', $user_alice)
->isNeutral());
$this
->assertTrue($flag
->actionAccess('unflag', $user_bob)
->isNeutral());
}
}