-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathDrupalGroup.php
More file actions
106 lines (95 loc) · 2.85 KB
/
DrupalGroup.php
File metadata and controls
106 lines (95 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace Codeception\Module;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\group\Entity\GroupInterface;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
/**
* Class DrupalGroup.
*
* ### Example
* #### Example (DrupalGroup)
* modules:
* - DrupalGroup:
* cleanup_test: true
* cleanup_failed: false
* cleanup_suite: true
* route_entities:
* - node
* - taxonomy_term.
*
* @package Codeception\Module
*/
class DrupalGroup extends DrupalEntity {
/**
* Wrapper method to create a group.
*
* Improves readbility of tests by having the method read "create group".
*/
public function createGroup(array $values = [], $validate = TRUE) {
$type = 'group';
if (!array_key_exists('uid', $values)) {
$values['uid'] = 1;
}
try {
$entity = \Drupal::entityTypeManager()
->getStorage($type)
->create($values);
if ($validate && $entity instanceof FieldableEntityInterface) {
$violations = $entity->validate();
if ($violations->count() > 0) {
$message = PHP_EOL;
foreach ($violations as $violation) {
$message .= $violation->getPropertyPath() . ': ' . $violation->getMessage() . PHP_EOL;
}
throw new \Exception($message);
}
}
// Group specific entity save options.
$entity->setOwner(User::load($values['uid'] ?? 1));
$entity->save();
}
catch (\Exception $e) {
$this->fail('Could not create group entity. Error message: ' . $e->getMessage());
}
if (!empty($entity)) {
$this->registerTestEntity($entity->getEntityTypeId(), $entity->id());
return $entity;
}
return FALSE;
}
/**
* Join the defined group.
*
* @param \Drupal\group\Entity\GroupInterface $group
* Instance of a group.
* @param \Drupal\user\UserInterface $user
* A drupal user to add to the group.
*
* @return \Drupal\group\GroupMembership|false
* Returns the group content entity, FALSE otherwise.
*/
public function joinGroup(GroupInterface $group, UserInterface $user) {
$group->addMember($user);
return $group->getMember($user);
}
/**
* Leave a group.
*
* @param \Drupal\group\Entity\GroupInterface $group
* Instance of a group.
* @param \Drupal\user\UserInterface $user
* A drupal user to add to the group.
*
* @return bool
* Returns the TRUE if the user is no longer a member of the group,
* FALSE otherwise.
*/
public function leaveGroup(GroupInterface $group, UserInterface $user) {
$group->removeMember($user);
// Get member should return FALSE if the user isn't a member so we
// reverse the logic. If they are still a member it'll cast to TRUE.
$is_member = (bool) $group->getMember($user);
return !$is_member;
}
}