Skip to content

Commit

Permalink
Issue backdrop-contrib#32: Get some more tests passing.
Browse files Browse the repository at this point in the history
More progress on this issue by @indigoxela.
  • Loading branch information
indigoxela authored Apr 8, 2022
1 parent ff9635f commit f92c8f6
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 161 deletions.
4 changes: 2 additions & 2 deletions includes/og.membership.inc
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ class OgMembership extends Entity {
// Clear the group content entity field cache.
cache_clear_all('field:' . $this->entity_type . ':' . $this->etid, 'cache_field');

// Supporting the entity cache module.
if (module_exists('entitycache') && db_table_exists('cache_entity_' . $this->entity_type)) {
// Supporting the entity cache.
if (db_table_exists('cache_entity_' . $this->entity_type)) {
cache_clear_all($this->etid, 'cache_entity_' . $this->entity_type);
}
}
Expand Down
7 changes: 7 additions & 0 deletions og.module
Original file line number Diff line number Diff line change
Expand Up @@ -2118,6 +2118,13 @@ function og_group($group_type, $gid, $values = array(), $save_created = TRUE) {
// Save the membership for update, or if the OG membership is new when
// "save-created" is TRUE.
$og_membership->save();

// Also populate the group audience field on the entity for data
// consistency.
// @see https://www.drupal.org/project/og/issues/2148047
if ($field_name) {
$wrapper->{$field_name}[] = $gid;
}
}

return $og_membership;
Expand Down
94 changes: 46 additions & 48 deletions og_ui/og_ui.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
class OgUiUserPermissionsTestCase extends BackdropWebTestCase {

function setUp() {
parent::setUp('og_ui', 'entity_feature');
parent::setUp('og_ui', 'og_entity_test');

// Add OG group fields.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
og_create_field(OG_GROUP_FIELD, 'og_entity_test', 'main');
}

/**
Expand All @@ -24,30 +24,30 @@ class OgUiUserPermissionsTestCase extends BackdropWebTestCase {
$this->backdropLogin($admin_user);

// Create a group.
$entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $admin_user->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity1);
$entity1 = entity_create('og_entity_test', array('name' => 'main', 'uid' => $admin_user->uid));
$wrapper = entity_metadata_wrapper('og_entity_test', $entity1);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();

$this->assertTrue(og_user_access('entity_test', $entity1->pid, 'subscribe', $web_user), t('User has "subscribe" permission.'));
$this->assertTrue(og_user_access('og_entity_test', $entity1->pid, 'subscribe', $web_user), t('User has "subscribe" permission.'));

// Remove a permission.
$this->backdropPost('admin/config/group/permissions/entity_test/main', array('1[subscribe]' => FALSE), t('Save permissions'));
$this->backdropPost('admin/config/group/permissions/og_entity_test/main', array('1[subscribe]' => FALSE), t('Save permissions'));
$this->assertText(t('The changes have been saved.'), t('Successful save message displayed.'));

// FIXME: There is an og_invalidate_cache() on permissions granting
// and revoking, but somehow, we need to do it manually here.
og_invalidate_cache();

$this->assertFalse(og_user_access('entity_test', $entity1->pid, 'subscribe', $web_user), t('User now does not have "subscribe" permission.'));
$this->assertFalse(og_user_access('og_entity_test', $entity1->pid, 'subscribe', $web_user), t('User now does not have "subscribe" permission.'));

// Re-add permission.
$this->backdropPost('admin/config/group/permissions/entity_test/main', array('1[subscribe]' => TRUE), t('Save permissions'));
$this->backdropPost('admin/config/group/permissions/og_entity_test/main', array('1[subscribe]' => TRUE), t('Save permissions'));

// FIXME: There is an og_invalidate_cache() on permissions granting
// and revoking, but somehow, we need to do it manually here.
og_invalidate_cache();
$this->assertTrue(og_user_access('entity_test', $entity1->pid, 'subscribe', $web_user), t('User has "subscribe" permission again.'));
$this->assertTrue(og_user_access('og_entity_test', $entity1->pid, 'subscribe', $web_user), t('User has "subscribe" permission again.'));
}
}

Expand All @@ -57,7 +57,7 @@ class OgUiAdminPermissionsTestCase extends BackdropWebTestCase {
parent::setUp('og_ui');

// Add OG group fields.
og_create_field(OG_GROUP_FIELD, 'node', 'article');
og_create_field(OG_GROUP_FIELD, 'node', 'post');
}

/**
Expand All @@ -69,7 +69,7 @@ class OgUiAdminPermissionsTestCase extends BackdropWebTestCase {

$settings = array();
$settings['uid'] = $user1->uid;
$settings['type'] = 'article';
$settings['type'] = 'post';
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
$node = $this->backdropCreateNode($settings);

Expand All @@ -86,7 +86,7 @@ class OgUiAdminPermissionsTestCase extends BackdropWebTestCase {
'manage permissions',
);

$roles = og_roles('node', 'article');
$roles = og_roles('node', 'post');
$auth_rid = array_search(OG_ANONYMOUS_ROLE, $roles);
foreach ($perms as $perm) {
// Add an admin permission to allow the user to access to the admin tabs.
Expand All @@ -108,12 +108,12 @@ class OgUiAdminPermissionsTestCase extends BackdropWebTestCase {

$settings = array();
$settings['uid'] = $user1->uid;
$settings['type'] = 'article';
$settings['type'] = 'post';
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
$node = $this->backdropCreateNode($settings);

$this->backdropLogin($user2);
$roles = og_roles('node', 'article');
$roles = og_roles('node', 'post');
$auth_rid = array_search(OG_ANONYMOUS_ROLE, $roles);
$text = t('Warning: Give to trusted roles only; this permission has security implications in the group context.');

Expand All @@ -135,9 +135,9 @@ class OgUiAdminPermissionsTestCase extends BackdropWebTestCase {
class OgUiSubscribeTestCase extends BackdropWebTestCase {

function setUp() {
parent::setUp('og_ui', 'entity_feature');
parent::setUp('og_ui', 'og_entity_test');
// Add OG group field.
og_create_field(OG_GROUP_FIELD, 'node', 'article');
og_create_field(OG_GROUP_FIELD, 'node', 'post');
}

/**
Expand All @@ -150,7 +150,7 @@ class OgUiSubscribeTestCase extends BackdropWebTestCase {

// Create a group.
$settings = array();
$settings['type'] = 'article';
$settings['type'] = 'post';
$settings['uid'] = $user1->uid;
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
$node = $this->backdropCreateNode($settings);
Expand Down Expand Up @@ -183,7 +183,7 @@ class OgUiSubscribeTestCase extends BackdropWebTestCase {
$this->assertFalse(og_is_member('node', $node->nid, 'user', $user2, array(OG_STATE_ACTIVE, OG_STATE_PENDING)), t('User unsubscribed from group.'));

// Change global permissions to allow user to subscribe without approval.
$og_roles = og_roles('node', 'article');
$og_roles = og_roles('node', 'post');
$rid = array_search(OG_ANONYMOUS_ROLE, $og_roles);
og_role_change_permissions($rid, array('subscribe without approval' => 1));

Expand All @@ -203,20 +203,21 @@ class OgUiManagePeopleTestCase extends BackdropWebTestCase {
public $entity;

function setUp() {
parent::setUp('og_ui', 'entity_feature');
// Add OG group field.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'test');
parent::setUp('og_ui', 'og_entity_test');

// Create users.
$this->user1 = $this->backdropCreateUser();
$this->user2 = $this->backdropCreateUser();

// Create a group.
$this->entity = entity_create('entity_test', array('name' => 'test', 'uid' => $this->user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $this->entity);
$entity = entity_create('og_entity_test', array('name' => 'test', 'uid' => $this->user1->uid));
// Add OG group field.
og_create_field(OG_GROUP_FIELD, 'og_entity_test', 'test');
$wrapper = entity_metadata_wrapper('og_entity_test', $entity);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();

$this->entity = $entity;
}

/**
Expand All @@ -226,23 +227,23 @@ class OgUiManagePeopleTestCase extends BackdropWebTestCase {
$this->backdropLogin($this->user1);

// Add user2 to the group.
$this->assertFalse(og_is_member('entity_test', $this->entity->pid, 'user', $this->user2), 'User is not a group member');
$this->assertFalse(og_is_member('og_entity_test', $this->entity->pid, 'user', $this->user2), 'User is not a group member');
$edit = array();
$edit['name'] = $this->user2->name;
$this->backdropPost('group/entity_test/' . $this->entity->pid . '/admin/people/add-user', $edit, t('Add users'));
$this->backdropPost('group/og_entity_test/' . $this->entity->pid . '/admin/people/add-user', $edit, t('Add users'));

// Reload user.
og_invalidate_cache();
$this->assertTrue(og_is_member('entity_test', $this->entity->pid, 'user', $this->user2), 'User was added to the group.');
$this->assertTrue(og_is_member('og_entity_test', $this->entity->pid, 'user', $this->user2), 'User was added to the group.');

// Add the same user twice.
$this->backdropPost('group/entity_test/' . $this->entity->pid . '/admin/people/add-user', $edit, t('Add users'));
$this->backdropPost('group/og_entity_test/' . $this->entity->pid . '/admin/people/add-user', $edit, t('Add users'));
$this->assertText(t('User @name is already subscribed to group.', array('@name' => user_format_name($this->user2))), 'User can not be added twice.');

// Add non-existing user.
$edit = array();
$edit['name'] = $this->randomName();
$this->backdropPost('group/entity_test/' . $this->entity->pid . '/admin/people/add-user', $edit, t('Add users'));
$this->backdropPost('group/og_entity_test/' . $this->entity->pid . '/admin/people/add-user', $edit, t('Add users'));
$this->assertText(t('You have entered an invalid user name.'), t('Invalid user name not added to group.'));
}

Expand All @@ -253,12 +254,12 @@ class OgUiManagePeopleTestCase extends BackdropWebTestCase {
$this->backdropLogin($this->user1);

// Group the user to the group.
$membership = og_group('entity_test', $this->entity->pid, array('entity' => $this->user2));
$membership = og_group('og_entity_test', $this->entity->pid, array('entity' => $this->user2));

// Updating the state status.
$states = og_group_content_states();
foreach ($states as $state => $title) {
$this->backdropPost('group/entity_test/' . $this->entity->pid . '/admin/people/edit-membership/' . $membership->id, array('state' => $state), t('Update membership'));
$this->backdropPost('group/og_entity_test/' . $this->entity->pid . '/admin/people/edit-membership/' . $membership->id, array('state' => $state), t('Update membership'));

// Reset the static cache for a fresh OG membership object.
backdrop_static_reset();
Expand All @@ -276,8 +277,8 @@ class OgUiManagePeopleTestCase extends BackdropWebTestCase {
$this->backdropLogin($this->user1);

// Group the user to the group.
$membership = og_group('entity_test', $this->entity->pid, array('entity' => $this->user2));
$this->backdropPost('group/entity_test/' . $this->entity->pid . '/admin/people/delete-membership/' . $membership->id, array(), t('Remove'));
$membership = og_group('og_entity_test', $this->entity->pid, array('entity' => $this->user2));
$this->backdropPost('group/og_entity_test/' . $this->entity->pid . '/admin/people/delete-membership/' . $membership->id, array(), t('Remove'));

// Verify the membership was removed.
$this->assertText('The membership was removed.');
Expand All @@ -290,17 +291,14 @@ class OgUiManagePeopleTestCase extends BackdropWebTestCase {
$user1 = $this->backdropCreateUser();
$this->backdropLogin($user1);

// Delete the default group audience field
field_delete_field('og_user_entity_test');

// Create three group audience fields and corresponding instances on users:
// - Two for the two bundles on the 'entity_test' entity type.
// - One for the 'entity_test2' entity type.
$fields['group_audience_entity_test_test'] = field_create_field(array(
'field_name' => 'group_audience_entity_test_test',
'type' => 'entityreference',
'settings' => array(
'target_type' => 'entity_test',
'target_type' => 'og_entity_test',
'handler' => 'og',
'handler_settings' => array(
'target_bundles' => array('test'),
Expand All @@ -318,7 +316,7 @@ class OgUiManagePeopleTestCase extends BackdropWebTestCase {
'field_name' => 'group_audience_entity_test_test2',
'type' => 'entityreference',
'settings' => array(
'target_type' => 'entity_test',
'target_type' => 'og_entity_test',
'handler' => 'og',
'handler_settings' => array(
'target_bundles' => array('test2'),
Expand All @@ -336,7 +334,7 @@ class OgUiManagePeopleTestCase extends BackdropWebTestCase {
'field_name' => 'group_audience_entity_test2',
'type' => 'entityreference',
'settings' => array(
'target_type' => 'entity_test2',
'target_type' => 'og_entity_test2',
'handler' => 'og',
'handler_settings' => array(
'membership_type' => 'og_membership_type_default',
Expand All @@ -351,21 +349,21 @@ class OgUiManagePeopleTestCase extends BackdropWebTestCase {

// Create a group belonging to the 'test' bundle of the 'entity_test' entity
// type.
$entity = entity_create('entity_test', array('name' => 'test', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity);
$entity = entity_create('og_entity_test', array('name' => 'test', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('og_entity_test', $entity);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();

// Because only one of the three fields applies to this entity type and
// bundle, no select box should be shown.
$this->backdropGet('group/entity_test/' . $entity->pid . '/admin/people/add-user');
$this->backdropGet('group/og_entity_test/' . $entity->pid . '/admin/people/add-user');
$this->assertNoField('edit-field-name');

// Temporarily change the second field to apply to this bundle. Now the
// select box should be shown.
$field['group_audience_entity_test_test2']['settings']['handler_settings']['target_bundles'] = array('test');
field_update_field($field['group_audience_entity_test_test2']);
$this->backdropGet('group/entity_test/' . $entity->pid . '/admin/people/add-user');
$this->backdropGet('group/og_entity_test/' . $entity->pid . '/admin/people/add-user');
$this->assertField('edit-field-name');
$elements = $this->xpath('//select[@id="edit-field-name"]//option');
$this->assertEqual(count($elements), 2, '2 options available for selection');
Expand All @@ -377,14 +375,14 @@ class OgUiManagePeopleTestCase extends BackdropWebTestCase {
// Revert the field settings to the previous state.
$field['group_audience_entity_test_test2']['settings']['handler_settings']['target_bundles'] = array('test2');
field_update_field($field['group_audience_entity_test_test2']);
$this->backdropGet('group/entity_test/' . $entity->pid . '/admin/people/add-user');
$this->backdropGet('group/og_entity_test/' . $entity->pid . '/admin/people/add-user');
$this->assertNoField('edit-field-name');

// Change the third field to apply to this entity type. In this case the
// select box should be shown, as well.
$field['group_audience_entity_test2']['settings']['target_type'] = 'entity_test';
field_update_field($field['group_audience_entity_test2']);
$this->backdropGet('group/entity_test/' . $entity->pid . '/admin/people/add-user');
$this->backdropGet('group/og_entity_test/' . $entity->pid . '/admin/people/add-user');
$this->assertField('edit-field-name');
$elements = $this->xpath('//select[@id="edit-field-name"]//option');
$this->assertEqual(count($elements), 2, '2 options available for selection');
Expand All @@ -398,18 +396,18 @@ class OgUiManagePeopleTestCase extends BackdropWebTestCase {
* Tests that invalid group IDs in the menu path do not cause exceptions.
*/
public function testOgUiPath() {
$this->backdropGet('entity_test/' . $this->entity->pid . 'invalid/group');
$this->backdropGet('og_entity_test/' . $this->entity->pid . 'invalid/group');
$this->assertResponse(403);
// Numeric values that are not consist of decimal characters are forbidden.
// 0x1 for instance is equivalent to 1
// http://php.net/manual/en/language.types.integer.php
$this->backdropGet('entity_test/0x' . $this->entity->pid . '/group');
$this->backdropGet('og_entity_test/0x' . $this->entity->pid . '/group');
$this->assertResponse(403);
// Non-existing groups return 404 however.
$this->backdropGet('entity_test/666');
$this->backdropGet('og_entity_test/666');
$this->assertResponse(404);
// For the same, admin area returns 403.
$this->backdropGet('entity_test/666/group');
$this->backdropGet('og_entity_test/666/group');
$this->assertResponse(403);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function OgMembershipCrud($entity_type, $entity, $field, $instance, $lang
* Array with all the differences, or an empty array if none found.
*/
public function groupAudiencegetDiff($entity_type, $entity, $field, $instance, $langcode, $items) {
$return = FALSE;
$return = array();

$field_name = $field['field_name'];
$wrapper = entity_metadata_wrapper($entity_type, $entity);
Expand Down
Loading

0 comments on commit f92c8f6

Please sign in to comment.