Chris@0: set('node_access_test.private', TRUE); Chris@0: Chris@0: // Add Hungarian, Catalan and Croatian. Chris@0: ConfigurableLanguage::createFromLangcode('hu')->save(); Chris@0: ConfigurableLanguage::createFromLangcode('ca')->save(); Chris@0: ConfigurableLanguage::createFromLangcode('hr')->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests node access with multiple node languages and no private nodes. Chris@0: */ Chris@0: public function testNodeAccess() { Chris@0: $web_user = $this->drupalCreateUser(['access content']); 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: // Creating a public node with langcode Hungarian, will be saved as the Chris@0: // fallback in node access table. Chris@0: $node_public_hu = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'hu', 'private' => FALSE]); Chris@0: $this->assertTrue($node_public_hu->language()->getId() == 'hu', 'Node created as Hungarian.'); Chris@0: Chris@0: // Tests the default access is provided for the public Hungarian node. Chris@0: $this->assertNodeAccess($expected_node_access, $node_public_hu, $web_user); Chris@0: Chris@0: // Tests that Hungarian provided specifically results in the same. Chris@0: $this->assertNodeAccess($expected_node_access, $node_public_hu->getTranslation('hu'), $web_user); Chris@0: Chris@0: // Creating a public node with no special langcode, like when no language Chris@0: // module enabled. Chris@0: $node_public_no_language = $this->drupalCreateNode([ Chris@0: 'private' => FALSE, Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: ]); Chris@0: $this->assertTrue($node_public_no_language->language()->getId() == LanguageInterface::LANGCODE_NOT_SPECIFIED, 'Node created with not specified language.'); Chris@0: Chris@0: // Tests that access is granted if requested with no language. Chris@0: $this->assertNodeAccess($expected_node_access, $node_public_no_language, $web_user); Chris@0: Chris@0: // Reset the node access cache and turn on our test node access code. Chris@0: \Drupal::entityManager()->getAccessControlHandler('node')->resetCache(); Chris@0: \Drupal::state()->set('node_access_test_secret_catalan', 1); Chris@0: $node_public_ca = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'ca', 'private' => FALSE]); Chris@0: $this->assertTrue($node_public_ca->language()->getId() == 'ca', 'Node created as Catalan.'); Chris@0: Chris@0: // Tests that access is granted if requested with no language. Chris@0: $this->assertNodeAccess($expected_node_access, $node_public_no_language, $web_user); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_public_ca, $web_user); Chris@0: Chris@0: // Tests that Hungarian node is still accessible. Chris@0: $this->assertNodeAccess($expected_node_access, $node_public_hu, $web_user); Chris@0: $this->assertNodeAccess($expected_node_access, $node_public_hu->getTranslation('hu'), $web_user); Chris@0: Chris@0: // Tests that Catalan is still not accessible. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_public_ca->getTranslation('ca'), $web_user); Chris@0: Chris@0: // Make Catalan accessible. Chris@0: \Drupal::state()->set('node_access_test_secret_catalan', 0); Chris@0: Chris@0: // Tests that Catalan is accessible on a node with a Catalan version as the Chris@0: // static cache has not been reset. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_public_ca, $web_user); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_public_ca->getTranslation('ca'), $web_user); Chris@0: Chris@0: \Drupal::entityManager()->getAccessControlHandler('node')->resetCache(); Chris@0: Chris@0: // Tests that access is granted if requested with no language. Chris@0: $this->assertNodeAccess($expected_node_access, $node_public_no_language, $web_user); Chris@0: $this->assertNodeAccess($expected_node_access, $node_public_ca, $web_user); Chris@0: Chris@0: // Tests that Hungarian node is still accessible. Chris@0: $this->assertNodeAccess($expected_node_access, $node_public_hu, $web_user); Chris@0: $this->assertNodeAccess($expected_node_access, $node_public_hu->getTranslation('hu'), $web_user); Chris@0: Chris@0: // Tests that Catalan is accessible on a node with a Catalan version. Chris@0: $this->assertNodeAccess($expected_node_access, $node_public_ca->getTranslation('ca'), $web_user); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests node access with multiple node languages and private nodes. Chris@0: */ Chris@0: public function testNodeAccessPrivate() { Chris@0: $web_user = $this->drupalCreateUser(['access content']); 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: // Creating a private node with langcode Hungarian, will be saved as the Chris@0: // fallback in node access table. Chris@0: $node_private_hu = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'hu', 'private' => TRUE]); Chris@0: $this->assertTrue($node_private_hu->language()->getId() == 'hu', 'Node created as Hungarian.'); Chris@0: Chris@0: // Tests the default access is not provided for the private Hungarian node. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_private_hu, $web_user); Chris@0: Chris@0: // Tests that Hungarian provided specifically results in the same. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_private_hu->getTranslation('hu'), $web_user); Chris@0: Chris@0: // Creating a private node with no special langcode, like when no language Chris@0: // module enabled. Chris@0: $node_private_no_language = $this->drupalCreateNode([ Chris@0: 'private' => TRUE, Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: ]); Chris@0: $this->assertTrue($node_private_no_language->language()->getId() == LanguageInterface::LANGCODE_NOT_SPECIFIED, 'Node created with not specified language.'); Chris@0: Chris@0: // Tests that access is not granted if requested with no language. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_private_no_language, $web_user); Chris@0: Chris@0: // Reset the node access cache and turn on our test node access code. Chris@0: \Drupal::entityManager()->getAccessControlHandler('node')->resetCache(); Chris@0: \Drupal::state()->set('node_access_test_secret_catalan', 1); Chris@0: Chris@0: // Tests that access is not granted if requested with no language. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_private_no_language, $web_user); Chris@0: Chris@0: // Creating a private node with langcode Catalan to test that the Chris@0: // node_access_test_secret_catalan flag works. Chris@0: $private_ca_user = $this->drupalCreateUser(['access content', 'node test view']); Chris@0: $node_private_ca = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'ca', 'private' => TRUE]); Chris@0: $this->assertTrue($node_private_ca->language()->getId() == 'ca', 'Node created as Catalan.'); Chris@0: Chris@0: // Tests that Catalan is still not accessible to either user. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_private_ca, $web_user); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_private_ca->getTranslation('ca'), $web_user); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_private_ca, $private_ca_user); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_private_ca->getTranslation('ca'), $private_ca_user); Chris@0: Chris@0: \Drupal::entityManager()->getAccessControlHandler('node')->resetCache(); Chris@0: \Drupal::state()->set('node_access_test_secret_catalan', 0); Chris@0: Chris@0: // Tests that Catalan is still not accessible for a user with no access to Chris@0: // private nodes. Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_private_ca, $web_user); Chris@0: $this->assertNodeAccess($expected_node_access_no_access, $node_private_ca->getTranslation('ca'), $web_user); Chris@0: Chris@0: // Tests that Catalan is accessible by a user with the permission to see Chris@0: // private nodes. Chris@0: $this->assertNodeAccess($expected_node_access, $node_private_ca, $private_ca_user); Chris@0: $this->assertNodeAccess($expected_node_access, $node_private_ca->getTranslation('ca'), $private_ca_user); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests db_select() with a 'node_access' tag and langcode metadata. Chris@0: */ Chris@0: public function testNodeAccessQueryTag() { Chris@0: // Create a normal authenticated user. Chris@0: $web_user = $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: $admin_user = User::load(1); Chris@0: Chris@0: // Creating a private node with langcode Hungarian, will be saved as Chris@0: // the fallback in node access table. Chris@0: $node_private = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'hu', 'private' => TRUE]); Chris@0: $this->assertTrue($node_private->language()->getId() == 'hu', 'Node created as Hungarian.'); Chris@0: Chris@0: // Creating a public node with langcode Hungarian, will be saved as Chris@0: // the fallback in node access table. Chris@0: $node_public = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'hu', 'private' => FALSE]); Chris@0: $this->assertTrue($node_public->language()->getId() == 'hu', 'Node created as Hungarian.'); Chris@0: Chris@0: // Creating a public node with no special langcode, like when no language Chris@0: // module enabled. Chris@0: $node_no_language = $this->drupalCreateNode([ Chris@0: 'private' => FALSE, Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: ]); Chris@0: $this->assertTrue($node_no_language->language()->getId() == LanguageInterface::LANGCODE_NOT_SPECIFIED, 'Node created with not specified language.'); Chris@0: Chris@18: $connection = Database::getConnection(); Chris@0: // Query the nodes table as the web user with the node access tag and no Chris@0: // specific langcode. Chris@18: $select = $connection->select('node', 'n') Chris@0: ->fields('n', ['nid']) Chris@0: ->addMetaData('account', $web_user) Chris@0: ->addTag('node_access'); Chris@0: $nids = $select->execute()->fetchAllAssoc('nid'); Chris@0: Chris@0: // The public node and no language node should be returned. Because no Chris@0: // langcode is given it will use the fallback node. Chris@0: $this->assertEqual(count($nids), 2, 'db_select() returns 2 node'); Chris@0: $this->assertTrue(array_key_exists($node_public->id(), $nids), 'Returned node ID is public node.'); Chris@0: $this->assertTrue(array_key_exists($node_no_language->id(), $nids), 'Returned node ID is no language node.'); Chris@0: Chris@0: // Query the nodes table as the web user with the node access tag and Chris@0: // langcode de. Chris@18: $select = $connection->select('node', 'n') Chris@0: ->fields('n', ['nid']) Chris@0: ->addMetaData('account', $web_user) Chris@0: ->addMetaData('langcode', 'de') Chris@0: ->addTag('node_access'); Chris@0: $nids = $select->execute()->fetchAllAssoc('nid'); Chris@0: Chris@0: // Because no nodes are created in German, no nodes 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', $admin_user) 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), 3, 'db_select() returns all three 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', $admin_user) Chris@0: ->addMetaData('langcode', 'de') Chris@0: ->addTag('node_access'); Chris@0: $nids = $select->execute()->fetchAllAssoc('nid'); Chris@0: Chris@0: // All nodes are returned because node access tag is not invoked when the Chris@0: // user is user 1. Chris@0: $this->assertEqual(count($nids), 3, 'db_select() returns all three nodes.'); Chris@0: } Chris@0: Chris@0: }