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: // 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: // Add Hungarian and Catalan. Chris@0: ConfigurableLanguage::createFromLangcode('hu')->save(); Chris@0: ConfigurableLanguage::createFromLangcode('ca')->save(); 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). Chris@0: Chris@0: // Create six nodes: Chris@0: // 1. Four Hungarian nodes with Catalan translations Chris@0: // - One with neither language marked as private. Chris@0: // - One with only the Hungarian translation private. Chris@0: // - One with only the Catalan translation private. Chris@0: // - One with both the Hungarian and Catalan translations private. Chris@0: // 2. Two nodes with no language specified. Chris@0: // - One public. Chris@0: // - One private. Chris@0: $this->nodes['both_public'] = $node = $this->drupalCreateNode([ Chris@0: 'body' => [[]], Chris@0: 'langcode' => 'hu', Chris@0: 'field_private' => [['value' => 0]], 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['ca_private'] = $node = $this->drupalCreateNode([ Chris@0: 'body' => [[]], Chris@0: 'langcode' => 'hu', Chris@0: 'field_private' => [['value' => 0]], 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['hu_private'] = $node = $this->drupalCreateNode([ Chris@0: 'body' => [[]], Chris@0: 'langcode' => 'hu', Chris@0: 'field_private' => [['value' => 1]], 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['both_private'] = $node = $this->drupalCreateNode([ Chris@0: 'body' => [[]], Chris@0: 'langcode' => 'hu', Chris@0: 'field_private' => [['value' => 1]], 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['no_language_public'] = $this->drupalCreateNode([ Chris@0: 'field_private' => [['value' => 0]], Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: ]); Chris@0: $this->nodes['no_language_private'] = $this->drupalCreateNode([ Chris@0: 'field_private' => [['value' => 1]], 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 testNodeAccessLanguageAware() { Chris@0: // The node_access_test_language module only grants view access. 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 both Hungarian and Catalan are marked as public, access to the Chris@0: // Hungarian translation should be granted with the default entity object or Chris@0: // when the Hungarian translation is specified explicitly. Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['both_public'], $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['both_public']->getTranslation('hu'), $this->webUser); Chris@0: // Access to the Catalan translation should also be granted. Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['both_public']->getTranslation('ca'), $this->webUser); Chris@0: Chris@0: // When Hungarian is marked as private, access to the Hungarian translation Chris@0: // should be denied with the default entity object or when the Hungarian Chris@0: // translation is specified explicitly. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['hu_private'], $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['hu_private']->getTranslation('hu'), $this->webUser); Chris@0: // Access to the Catalan translation should be granted. Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['hu_private']->getTranslation('ca'), $this->webUser); Chris@0: Chris@0: // When Catalan is marked as private, access to the Hungarian translation Chris@0: // should be granted with the default entity object or when the Hungarian Chris@0: // translation is specified explicitly. Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['ca_private'], $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['ca_private']->getTranslation('hu'), $this->webUser); Chris@0: // Access to the Catalan translation should be granted. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['ca_private']->getTranslation('ca'), $this->webUser); Chris@0: Chris@0: // When both translations are marked as private, access should be denied Chris@0: // regardless of the entity object specified. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['both_private'], $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['both_private']->getTranslation('hu'), $this->webUser); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['both_private']->getTranslation('ca'), $this->webUser); Chris@0: Chris@0: // When no language is specified for a private node, access to every node Chris@0: // translation is denied. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['no_language_private'], $this->webUser); Chris@0: Chris@0: // When no language is specified for a public node, access should be Chris@0: // granted. Chris@0: $this->assertNodeAccess($expected_node_access, $this->nodes['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) 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: // Three nodes should be returned: Chris@0: // - Node with both translations public. Chris@0: // - Node with only the Catalan translation marked as private. Chris@0: // - No language node marked as public. Chris@0: $this->assertEqual(count($nids), 3, 'db_select() returns 3 nodes when no langcode is specified.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['both_public']->id(), $nids), 'The node with both translations public is returned.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['ca_private']->id(), $nids), 'The node with only the Catalan translation private is returned.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['no_language_public']->id(), $nids), 'The node with no language is returned.'); 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: // Two nodes should be returned: the node with both translations public, and Chris@0: // the node with only the Catalan translation marked as private. Chris@0: $this->assertEqual(count($nids), 2, 'db_select() returns 2 nodes when the hu langcode is specified.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['both_public']->id(), $nids), 'The node with both translations public is returned.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['ca_private']->id(), $nids), 'The node with only the Catalan translation private is returned.'); 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: // Two nodes should be returned: the node with both translations public, and Chris@0: // the node with only the Hungarian translation marked as private. Chris@0: $this->assertEqual(count($nids), 2, 'db_select() returns 2 nodes when the hu langcode is specified.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['both_public']->id(), $nids), 'The node with both translations public is returned.'); Chris@0: $this->assertTrue(array_key_exists($this->nodes['hu_private']->id(), $nids), 'The node with only the Hungarian translation private is returned.'); 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 when the de langcode is specified.'); 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), 6, '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), 6, 'db_select() returns all nodes.'); Chris@0: } Chris@0: Chris@0: }