Skip to content
Snippets Groups Projects
Commit c011f137 authored by Eirik Morland's avatar Eirik Morland
Browse files

Issue #3345888 by eiriksm: Return instance of self to allow chaining of team node methods

parent e3aedc8b
No related branches found
No related tags found
1 merge request!5Issue #3345888: Return instance of self to allow chaining of team node methods
......@@ -37,7 +37,7 @@ class TeamNode extends Node {
* If you are trying to append a user that is already in there, it will only
* be referenced once.
*/
public function appendMember(UserInterface $user) {
public function appendMember(UserInterface $user) : self {
if (!$user->id()) {
throw new \InvalidArgumentException('Can not reference user id 0 when setting members');
}
......@@ -46,18 +46,20 @@ class TeamNode extends Node {
return $user->id() === $user_item->id();
});
if ($already_has_it) {
return;
return $this;
}
$members[] = $user;
$this->setMembers($members);
return $this;
}
/**
* Set all of the members of the node.
*/
public function setMembers(array $members) {
public function setMembers(array $members) : self {
$this->get(self::MEMBERS_FIELD)
->setValue($members);
return $this;
}
/**
......
......@@ -3,6 +3,7 @@
namespace Drupal\Tests\violinist_teams\Kernel;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
use Drupal\violinist_teams\TeamNode;
/**
......@@ -22,4 +23,22 @@ class TeamNodeTest extends KernelTestBase {
self::assertInstanceOf(TeamNode::class, $node);
}
/**
* Test append member method.
*/
public function testAppendMember() {
/** @var \Drupal\violinist_teams\TeamNode $node */
$node = Node::create([
'type' => $this->nodeType->id(),
]);
$user = User::create([
'name' => 'test',
'mail' => 'test@example.com',
]);
$user->save();
$team = $node->appendMember($user);
self::assertInstanceOf(TeamNode::class, $team);
self::assertCount(1, $team->getMembers());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment