View source
<?php
namespace Drupal\Tests\user_badges\Kernel\Entity;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\user_badges\Entity\Badge;
class BadgeRoleTest extends KernelTestBase {
public static $modules = [
'user',
'system',
'user_badges',
'field',
'options',
'file',
'image',
];
protected $rids = [];
protected $badgeIds = [];
protected function setUp() {
parent::setUp();
$this
->installConfig([
'user_badges',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('badge');
$this
->installSchema('system', 'sequences');
$role = Role::create([
'id' => $this
->randomMachineName(),
]);
$role
->save();
$this->rids[] = $role
->id();
$role = Role::create([
'id' => $this
->randomMachineName(),
]);
$role
->save();
$this->rids[] = $role
->id();
foreach ([
[],
$this->rids[0],
$this->rids,
] as $rids) {
$badge = Badge::create([
'type' => 'image_badge',
'name' => $this
->randomString(),
'role_id' => $rids,
]);
$badge
->save();
$this->badgeIds[] = $badge
->id();
}
}
public function testUserPresave() {
$user = User::create([
'name' => $this
->randomMachineName(),
]);
$user
->save();
$item_list = $user
->get('field_user_badges');
$this
->assertTrue($item_list
->isEmpty());
$user
->addRole($this->rids[1]);
$user
->save();
$this
->assertEquals($item_list
->getValue(), [
[
'target_id' => $this->badgeIds[2],
],
]);
$user
->removeRole($this->rids[1]);
$user
->save();
$this
->assertTrue($item_list
->isEmpty());
$user
->addRole($this->rids[0]);
$user
->save();
$this
->assertEquals($item_list
->getValue(), [
[
'target_id' => $this->badgeIds[1],
],
[
'target_id' => $this->badgeIds[2],
],
]);
$item_list
->appendItem($this->badgeIds[0]);
$this
->assertEquals($item_list
->count(), 3);
for ($i = 0; $i < 3; $i++) {
$this
->assertEquals($item_list
->get($i)
->getValue(), [
'target_id' => $this->badgeIds[($i + 1) % 3],
]);
}
$user
->save();
$this
->assertEquals($item_list
->count(), 3);
for ($i = 0; $i < 3; $i++) {
$this
->assertEquals($item_list
->get($i)
->getValue(), [
'target_id' => $this->badgeIds[($i + 1) % 3],
]);
}
}
}