Chris@0: 'field_private', Chris@0: 'entity_type' => 'node', Chris@0: 'type' => 'boolean', Chris@0: 'cardinality' => 1, Chris@0: ]); Chris@0: $field_storage->save(); Chris@0: Chris@0: FieldConfig::create([ Chris@0: 'field_storage' => $field_storage, Chris@0: 'bundle' => 'page', Chris@0: 'widget' => [ Chris@0: 'type' => 'options_buttons', Chris@0: ], Chris@0: 'settings' => [ Chris@0: 'on_label' => 'Private', Chris@0: 'off_label' => 'Not private', Chris@0: ], Chris@0: ])->save(); Chris@0: Chris@0: // After enabling a node access module, the access table has to be rebuild. Chris@0: node_access_rebuild(); Chris@0: Chris@0: // Add Hungarian and Catalan. Chris@0: ConfigurableLanguage::createFromLangcode('hu')->save(); Chris@0: ConfigurableLanguage::createFromLangcode('ca')->save(); Chris@0: Chris@0: // Create a normal authenticated user. Chris@0: $this->webUser = $this->drupalCreateUser(['access content']); Chris@0: Chris@0: // Load the user 1 user for later use as an admin user with permission to Chris@0: // see everything. Chris@0: $this->adminUser = User::load(1); Chris@0: Chris@0: // The node_access_test_language module allows individual translations of a Chris@0: // node to be marked private (not viewable by normal users), and the Chris@0: // node_access_test module allows whole nodes to be marked private. (In a Chris@0: // real-world implementation, hook_node_access_records_alter() might be Chris@0: // implemented by one or both modules to enforce that private nodes or Chris@0: // translations are always private, but we want to test the default, Chris@0: // additive behavior of node access). Chris@0: Chris@0: // Create six Hungarian nodes with Catalan translations: Chris@0: // 1. One public with neither language marked as private. Chris@0: // 2. One private with neither language marked as private. Chris@0: // 3. One public with only the Hungarian translation private. Chris@0: // 4. One public with only the Catalan translation private. Chris@0: // 5. One public with both the Hungarian and Catalan translations private. Chris@0: // 6. One private with both the Hungarian and Catalan translations private. Chris@0: $this->nodes['public_both_public'] = $node = $this->drupalCreateNode([ Chris@0: 'body' => [[]], Chris@0: 'langcode' => 'hu', Chris@0: 'field_private' => [['value' => 0]], Chris@0: 'private' => FALSE, Chris@0: ]); Chris@0: $translation = $node->addTranslation('ca'); Chris@0: $translation->title->value = $this->randomString(); Chris@0: $translation->field_private->value = 0; Chris@0: $node->save(); Chris@0: Chris@0: $this->nodes['private_both_public'] = $node = $this->drupalCreateNode([ Chris@0: 'body' => [[]], Chris@0: 'langcode' => 'hu', Chris@0: 'field_private' => [['value' => 0]], Chris@0: 'private' => TRUE, Chris@0: ]); Chris@0: $translation = $node->addTranslation('ca'); Chris@0: $translation->title->value = $this->randomString(); Chris@0: $translation->field_private->value = 0; Chris@0: $node->save(); Chris@0: Chris@0: $this->nodes['public_hu_private'] = $node = $this->drupalCreateNode([ Chris@0: 'body' => [[]], Chris@0: 'langcode' => 'hu', Chris@0: 'field_private' => [['value' => 1]], Chris@0: 'private' => FALSE, Chris@0: ]); Chris@0: $translation = $node->addTranslation('ca'); Chris@0: $translation->title->value = $this->randomString(); Chris@0: $translation->field_private->value = 0; Chris@0: $node->save(); Chris@0: Chris@0: $this->nodes['public_ca_private'] = $node = $this->drupalCreateNode([ Chris@0: 'body' => [[]], Chris@0: 'langcode' => 'hu', Chris@0: 'field_private' => [['value' => 0]], Chris@0: 'private' => FALSE, Chris@0: ]); Chris@0: $translation = $node->addTranslation('ca'); Chris@0: $translation->title->value = $this->randomString(); Chris@0: $translation->field_private->value = 1; Chris@0: $node->save(); Chris@0: Chris@0: $this->nodes['public_both_private'] = $node = $this->drupalCreateNode([ Chris@0: 'body' => [[]], Chris@0: 'langcode' => 'hu', Chris@0: 'field_private' => [['value' => 1]], Chris@0: 'private' => FALSE, Chris@0: ]); Chris@0: $translation = $node->addTranslation('ca'); Chris@0: $translation->title->value = $this->randomString(); Chris@0: $translation->field_private->value = 1; Chris@0: $node->save(); Chris@0: Chris@0: $this->nodes['private_both_private'] = $node = $this->drupalCreateNode([ Chris@0: 'body' => [[]], Chris@0: 'langcode' => 'hu', Chris@0: 'field_private' => [['value' => 1]], Chris@0: 'private' => TRUE, Chris@0: ]); Chris@0: $translation = $node->addTranslation('ca'); Chris@0: $translation->title->value = $this->randomString(); Chris@0: $translation->field_private->value = 1; Chris@0: $node->save(); Chris@0: Chris@0: $this->nodes['public_no_language_private'] = $this->drupalCreateNode([ Chris@0: 'field_private' => [['value' => 1]], Chris@0: 'private' => FALSE, Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: ]); Chris@0: $this->nodes['public_no_language_public'] = $this->drupalCreateNode([ Chris@0: 'field_private' => [['value' => 0]], Chris@0: 'private' => FALSE, Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: ]); Chris@0: $this->nodes['private_no_language_private'] = $this->drupalCreateNode([ Chris@0: 'field_private' => [['value' => 1]], Chris@0: 'private' => TRUE, Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: ]); Chris@0: $this->nodes['private_no_language_public'] = $this->drupalCreateNode([ Chris@0: 'field_private' => [['value' => 1]], Chris@0: 'private' => TRUE, Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: ]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests node access and node access queries with multiple node languages. Chris@0: */ Chris@0: public function testNodeAccessLanguageAwareCombination() { Chris@0: Chris@0: $expected_node_access = ['view' => TRUE, 'update' => FALSE, 'delete' => FALSE]; Chris@0: $expected_node_access_no_access = ['view' => FALSE, 'update' => FALSE, 'delete' => FALSE]; Chris@0: Chris@0: // When the node and both translations are public, access should always be Chris@0: // granted. Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['public_both_public'], $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['public_both_public']->getTranslation('hu'), $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['public_both_public']->getTranslation('ca'), $this->webUser); Chris@0: Chris@0: // If the node is marked private but both existing translations are not, Chris@0: // access should still be granted, because the grants are additive. Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['private_both_public'], $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['private_both_public']->getTranslation('hu'), $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['private_both_public']->getTranslation('ca'), $this->webUser); Chris@0: Chris@0: // If the node is marked private, but a existing translation is public, Chris@0: // access should only be granted for the public translation. With the Chris@0: // Hungarian translation marked as private, but the Catalan translation Chris@0: // public, the access is granted. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_hu_private'], $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_hu_private']->getTranslation('hu'), $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['public_hu_private']->getTranslation('ca'), $this->webUser); Chris@0: Chris@0: // With the Catalan translation marked as private, but the node public, Chris@0: // access is granted for the existing Hungarian translation, but not for the Chris@0: // Catalan. Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['public_ca_private'], $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['public_ca_private']->getTranslation('hu'), $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_ca_private']->getTranslation('ca'), $this->webUser); Chris@0: Chris@0: // With both translations marked as private, but the node public, access Chris@0: // should be denied in all cases. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_both_private'], $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_both_private']->getTranslation('hu'), $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_both_private']->getTranslation('ca'), $this->webUser); Chris@0: Chris@0: // If the node and both its existing translations are private, access should Chris@0: // be denied in all cases. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['private_both_private'], $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['private_both_private']->getTranslation('hu'), $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['private_both_private']->getTranslation('ca'), $this->webUser); Chris@0: Chris@0: // No access for all languages as the language aware node access module Chris@0: // denies access. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['public_no_language_private'], $this->webUser); Chris@0: Chris@0: // Access only for request with no language defined. Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['public_no_language_public'], $this->webUser); Chris@0: Chris@0: // No access for all languages as both node access modules deny access. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['private_no_language_private'], $this->webUser); Chris@0: Chris@0: // No access for all languages as the non language aware node access module Chris@0: // denies access. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['private_no_language_public'], $this->webUser); Chris@0: Chris@0: // Query the node table with the node access tag in several languages. Chris@18: $connection = Database::getConnection(); Chris@0: // Query with no language specified. The fallback (hu or und) will be used. Chris@18: $select = $connection->select('node', 'n') Chris@0: ->fields('n', ['nid']) Chris@0: ->addMetaData('account', $this->webUser) Chris@0: ->addTag('node_access'); Chris@0: $nids = $select->execute()->fetchAllAssoc('nid'); Chris@0: Chris@0: // Four nodes should be returned with public Hungarian translations or the Chris@0: // no language public node. Chris@0: $this->assertEqual(count($nids), 4, 'db_select() returns 4 nodes when no langcode is specified.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['public_both_public']->id(), $nids), 'Returned node ID is full public node.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['public_ca_private']->id(), $nids), 'Returned node ID is Hungarian public only node.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['private_both_public']->id(), $nids), 'Returned node ID is both public non-language-aware private only node.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['public_no_language_public']->id(), $nids), 'Returned node ID is no language public node.'); Chris@0: Chris@0: // Query with Hungarian (hu) specified. Chris@18: $select = $connection->select('node', 'n') Chris@0: ->fields('n', ['nid']) Chris@0: ->addMetaData('account', $this->webUser) Chris@0: ->addMetaData('langcode', 'hu') Chris@0: ->addTag('node_access'); Chris@0: $nids = $select->execute()->fetchAllAssoc('nid'); Chris@0: Chris@0: // Three nodes should be returned (with public Hungarian translations). Chris@0: $this->assertEqual(count($nids), 3, 'db_select() returns 3 nodes.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['public_both_public']->id(), $nids), 'Returned node ID is both public node.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['public_ca_private']->id(), $nids), 'Returned node ID is Hungarian public only node.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['private_both_public']->id(), $nids), 'Returned node ID is both public non-language-aware private only node.'); Chris@0: Chris@0: // Query with Catalan (ca) specified. Chris@18: $select = $connection->select('node', 'n') Chris@0: ->fields('n', ['nid']) Chris@0: ->addMetaData('account', $this->webUser) Chris@0: ->addMetaData('langcode', 'ca') Chris@0: ->addTag('node_access'); Chris@0: $nids = $select->execute()->fetchAllAssoc('nid'); Chris@0: Chris@0: // Three nodes should be returned (with public Catalan translations). Chris@0: $this->assertEqual(count($nids), 3, 'db_select() returns 3 nodes.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['public_both_public']->id(), $nids), 'Returned node ID is both public node.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['public_hu_private']->id(), $nids), 'Returned node ID is Catalan public only node.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['private_both_public']->id(), $nids), 'Returned node ID is both public non-language-aware private only node.'); Chris@0: Chris@0: // Query with German (de) specified. Chris@18: $select = $connection->select('node', 'n') Chris@0: ->fields('n', ['nid']) Chris@0: ->addMetaData('account', $this->webUser) Chris@0: ->addMetaData('langcode', 'de') Chris@0: ->addTag('node_access'); Chris@0: $nids = $select->execute()->fetchAllAssoc('nid'); Chris@0: Chris@0: // There are no nodes with German translations, so no results are returned. Chris@0: $this->assertTrue(empty($nids), 'db_select() returns an empty result.'); Chris@0: Chris@0: // Query the nodes table as admin user (full access) with the node access Chris@0: // tag and no specific langcode. Chris@18: $select = $connection->select('node', 'n') Chris@0: ->fields('n', ['nid']) Chris@0: ->addMetaData('account', $this->adminUser) Chris@0: ->addTag('node_access'); Chris@0: $nids = $select->execute()->fetchAllAssoc('nid'); Chris@0: Chris@0: // All nodes are returned. Chris@0: $this->assertEqual(count($nids), 10, 'db_select() returns all nodes.'); Chris@0: Chris@0: // Query the nodes table as admin user (full access) with the node access Chris@0: // tag and langcode de. Chris@18: $select = $connection->select('node', 'n') Chris@0: ->fields('n', ['nid']) Chris@0: ->addMetaData('account', $this->adminUser) Chris@0: ->addMetaData('langcode', 'de') Chris@0: ->addTag('node_access'); Chris@0: $nids = $select->execute()->fetchAllAssoc('nid'); Chris@0: Chris@0: // Even though there is no German translation, all nodes are returned Chris@0: // because node access filtering does not occur when the user is user 1. Chris@0: $this->assertEqual(count($nids), 10, 'db_select() returns all nodes.'); Chris@0: } Chris@0: Chris@0: }