diff --git a/forms/group_entity.inc b/forms/group_entity.inc index 20226dd..b304668 100644 --- a/forms/group_entity.inc +++ b/forms/group_entity.inc @@ -23,6 +23,18 @@ function group_entity_node_form_alter(&$form, &$form_state) { $options[$group->gid] = $group->title; } + //Set content to be private by default. + $required = TRUE; + $permissions = array_keys(node_list_permissions($node->type)); + foreach ($permissions as $permission) { + //If current editor have at least one of default node permission, it could + //publish public node or change it to be private. + if (user_access($permission)) { + $required = FALSE; + break; + } + } + //Check that if node is attach to a group, be sure current user is member of //this group. If occured, this is an unexpected bug. $disabled = ($groups_node && !array_intersect_key($groups_account, $groups_node)) ? TRUE : FALSE; @@ -37,6 +49,7 @@ function group_entity_node_form_alter(&$form, &$form_state) { '#empty_value' => 0, '#default_value' => (!empty($groups_node)) ? key($groups_node) : 0, '#disabled' => $disabled, + '#required' => $required, ); } } diff --git a/tests/group_node_access_ui.test b/tests/group_node_access_ui.test index 8116b94..42a1a26 100644 --- a/tests/group_node_access_ui.test +++ b/tests/group_node_access_ui.test @@ -70,11 +70,11 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { //Create a default admin member, reload default group roles to get content //type group permissions. entity_defaults_rebuild(); - $this->groupMemberAdmin = $this->createGroupMember($this->group, array('group_admin'), array("edit any $node_type content")); + $this->groupMemberAdmin = $this->createGroupMember($this->group, array('group_admin')); } /** - * @see GroupNodeAccessTestCase::testMain() + * @see GroupNodeAccessTestCase::testMain(). */ public function testMain() { @@ -88,18 +88,21 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { /** * Test : - * - field exists for member and default to public when creating. + * - field exists for member. * - create a public node by selecting no group option. * - outsider could view the public node previously created. */ protected function _testCreatePublicNode() { - $this->drupalLogin($this->groupMemberAdmin); + $node_type = $this->nodeType->type; + + $member = $this->createGroupMember($this->group, array(), array("create $node_type content")); + $this->drupalLogin($member); //Check that node edit form have the group extra field. - $path = 'node/add/' . str_replace('_', '-', $this->nodeType->type); + $path = 'node/add/' . str_replace('_', '-', $node_type); $this->drupalGet($path); - $this->assertFieldByName(GROUP_EXTRA_FIELD_GROUP, 0, t('Field group exists and set to default.')); + $this->assertFieldByName(GROUP_EXTRA_FIELD_GROUP, NULL, t('Field group exists.')); //Prepare node form and submit it. $edit = array(); @@ -111,7 +114,7 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { $url_parts = explode('/', $url); $nid = array_pop($url_parts); - //Check that anonymous could view public node. + //Check that an another outsider account could view public node. $account = $this->drupalCreateUser(array('access content')); $this->drupalLogin($account); $path = 'node/' . $nid; @@ -122,6 +125,7 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { /** * Test : + * - field group exists * - a member could create a private node by selecting one of its groups * - outsider could not view the private node * - member of another group could not view the private node @@ -130,12 +134,15 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { $this->drupalLogin($this->groupMemberAdmin); + $path = 'node/add/' . str_replace('_', '-', $this->nodeType->type); + $this->drupalGet($path); + $this->assertFieldByName(GROUP_EXTRA_FIELD_GROUP, NULL, t('Field group exists.')); + $edit = array(); $edit['title'] = $this->randomString(); $edit[GROUP_EXTRA_FIELD_GROUP] = $this->group->gid; - $path = 'node/add/' . str_replace('_', '-', $this->nodeType->type); - $this->drupalPost($path, $edit, t('Save')); + $this->drupalPost(NULL, $edit, t('Save')); //Retrieve nid from url after redirect. $url = $this->getUrl(); @@ -178,21 +185,16 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { $xpath = $this->constructFieldXpath('name', GROUP_EXTRA_FIELD_GROUP); $fields = $this->xpath($xpath); if ($fields) { - //Iterate over each potential fields with the same name. - foreach ($fields as $field) { - if (isset($field->option)) { - $items = $this->getAllOptions($field); - if ($items) { - $values = array(); - foreach ($items as $item) { - $values[] = (int)$item['value']; - } - //Check that all groups of the user are exposed - if (count(array_intersect(array($this->group->gid, $group->gid), $values)) === 2) { - $found = TRUE; - break; - } - } + //We presume that we only have one field with this name. + $items = $this->getAllOptions($fields[0]); + if ($items) { + $values = array(); + foreach ($items as $item) { + $values[] = (int)$item['value']; + } + //Check that all groups of the user are exposed + if (count(array_intersect(array($this->group->gid, $group->gid), $values)) === 2) { + $found = TRUE; } } } @@ -209,9 +211,11 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { */ protected function _testUpdatePublicNode() { - $node = $this->drupalCreateNode(array('type' => $this->nodeType->type)); + $node_type = $this->nodeType->type; + $node = $this->drupalCreateNode(array('type' => $node_type)); - $this->drupalLogin($this->groupMemberAdmin); + $account = $this->createGroupMember($this->group, array(), array("edit any $node_type content")); + $this->drupalLogin($account); $this->drupalGet('node/' . $node->nid . '/edit'); $this->assertFieldByName(GROUP_EXTRA_FIELD_GROUP, 0, t('Field group exists and have still no value.')); @@ -242,9 +246,8 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { */ protected function _testUpdatePrivateNodeNoGroupNoField() { - //Create a user with bypass access content and create - $node_type = $this->nodeType->type; - $account = $this->drupalCreateUser(array('bypass node access', "create $node_type content")); + //Create a user with bypass access content + $account = $this->drupalCreateUser(array('bypass node access')); $this->drupalLogin($account); @@ -263,17 +266,17 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { $node_type = $this->nodeType->type; - //Create a new group and a new member with some node permissions. - $group = $this->createGroup($this->groupType); - $member = $this->createGroupMember($group, array(), array('bypass node access', "create $node_type content")); - - //Create a private node of another group and by another author. + //Create a private node. $node = $this->drupalCreateNode(array( 'type' => $node_type, 'uid' => $this->groupMemberAdmin->uid, )); $this->group->addEntity($node->nid, 'node', $node->type); + //Create a new group and a new member with some node permissions. + $group = $this->createGroup($this->groupType); + $member = $this->createGroupMember($group, array(), array('bypass node access', "create $node_type content")); + //Check that field is disabled. $this->drupalLogin($member); @@ -283,11 +286,9 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { $xpath = $this->constructFieldXpath('name', GROUP_EXTRA_FIELD_GROUP); $fields = $this->xpath($xpath); if ($fields) { - //Iterate over each potential fields with the same name. - foreach ($fields as $field) { - if (isset($field->option) && !empty($field['disabled']) && (string)$field['disabled'] === 'disabled') { - $found = TRUE; - } + //We presume that we only have one field with this name. + if (!empty($fields[0]['disabled']) && (string)$fields[0]['disabled'] === 'disabled') { + $found = TRUE; } } $this->assertTrue($found, t('Field group found and disabled.')); @@ -296,6 +297,111 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { } /** + * Test that field group is not required for a public node if current author + * have at least one of the node permission and field is not disabled. + */ + protected function _testUpdatePublicNodeFieldGroupNotRequired() { + + $node_type = $this->nodeType->type; + + //Create a public node with a member. + $node = $this->drupalCreateNode(array( + 'type' => $node_type, + 'uid' => $this->groupMemberAdmin->uid + )); + + //Create a member of the group with node permission. + $member = $this->createGroupMember($this->group, array(), array("edit any $node_type content")); + $this->drupalLogin($member); + $this->drupalGet('node/' . $node->nid . '/edit'); + + $found = $required = FALSE; + $xpath = $this->constructFieldXpath('name', GROUP_EXTRA_FIELD_GROUP); + $fields = $this->xpath($xpath); + if ($fields) { + $found = TRUE; + //We presume that we only have one field with this name. + //Check that class "required" doesn't exists for this element. + if (!empty($fields[0]['class']) && strpos((string)$fields[0]['class'], 'required') !== FALSE) { + $required = TRUE; + } + } + $this->assertTrue($found && !$required, t('Field group found and is not required.')); + + $this->drupalLogout(); + } + + /** + * Test that field group is not required for a private node if current author + * have at least one of the node permission and field is not disabled. + */ + protected function _testUpdatePrivateNodeFieldGroupNotRequired() { + + $node_type = $this->nodeType->type; + + //Create a private node with a member. + $node = $this->drupalCreateNode(array( + 'type' => $node_type, + 'uid' => $this->groupMemberAdmin->uid + )); + $this->group->addEntity($node->nid, 'node', $node->type); + + //Create a member of the group with node permission. + $member = $this->createGroupMember($this->group, array('group_admin'), array("edit any $node_type content")); + $this->drupalLogin($member); + $this->drupalGet('node/' . $node->nid . '/edit'); + + $found = $required = FALSE; + $xpath = $this->constructFieldXpath('name', GROUP_EXTRA_FIELD_GROUP); + $fields = $this->xpath($xpath); + if ($fields) { + $found = TRUE; + //We presume that we only have one field with this name. + //Check that class "required" doesn't exists for this element. + if (!empty($fields[0]['class']) && strpos((string)$fields[0]['class'], 'required') !== FALSE) { + $required = TRUE; + } + } + $this->assertTrue($found && !$required, t('Field group found and is not required.')); + + $this->drupalLogout(); + } + + /** + * Test that field group is required for a private node if current author + * have no node permission and field is not disabled. + */ + protected function _testUpdatePrivateNodeFieldGroupRequired() { + + $node_type = $this->nodeType->type; + + //Create a private node with a member. + $node = $this->drupalCreateNode(array( + 'type' => $node_type, + 'uid' => $this->groupMemberAdmin->uid, + )); + $this->group->addEntity($node->nid, 'node', $node->type); + + $this->drupalLogin($this->groupMemberAdmin); + $this->drupalGet('node/' . $node->nid . '/edit'); + + $found = $required = FALSE; + $xpath = $this->constructFieldXpath('name', GROUP_EXTRA_FIELD_GROUP); + $fields = $this->xpath($xpath); + if ($fields) { + $found = TRUE; + //We presume that we only have one field with this name. + //Check that class "required" exists for this element. + if (!empty($fields[0]['class']) && strpos((string)$fields[0]['class'], 'required') !== FALSE) { + $required = TRUE; + } + } + $this->assertTrue($found && $required, t('Field group found and is required.')); + + $this->drupalLogout(); + } + + /** * Test switch accessibility from public to private */ protected function _testUpdatePublicNodeToPrivate () { @@ -303,7 +409,8 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { //Create a public node $node = $this->drupalCreateNode(array('type' => $this->nodeType->type)); - $this->drupalLogin($this->groupMemberAdmin); + $member = $this->createGroupMember($this->group, array(), array("edit any $node->type content")); + $this->drupalLogin($member); //Switch node to be private. $edit = array(); @@ -313,9 +420,8 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { $path = 'node/' . $node->nid; - //Create a new member to check access - $member = $this->createGroupMember($this->group); - $this->drupalLogin($member); + //Check access with another member + $this->drupalLogin($this->groupMemberAdmin); $this->drupalGet($path); $this->assertResponse(200, t('Member of the group could view the private node.')); $this->drupalLogout(); @@ -348,7 +454,8 @@ class GroupNodeAccessUITestCase extends GroupTestHelper { )); $this->group->addEntity($node->nid, 'node', $node->type); - $this->drupalLogin($this->groupMemberAdmin); + $member = $this->createGroupMember($this->group, array('group_admin'), array("edit any $node->type content")); + $this->drupalLogin($member); //Switch node to be public. $edit = array();