Chris@12: save(); Chris@12: ConfigurableLanguage::createFromLangcode('ca')->save(); Chris@12: ConfigurableLanguage::createFromLangcode('af')->save(); Chris@12: Chris@12: // Enable content translation for the current entity type. Chris@12: \Drupal::service('content_translation.manager')->setEnabled('node', 'page', TRUE); Chris@12: } Chris@12: Chris@12: /** Chris@12: * Tests node access fallback handling with multiple node languages. Chris@12: */ Chris@12: public function testNodeAccessLanguageFallback() { Chris@12: // The node_access_test module allows nodes to be marked private. We need to Chris@12: // ensure that system honors the fallback system of node access properly. Chris@12: // Note that node_access_test_language is language-sensitive and does not Chris@12: // apply to the fallback test. Chris@12: Chris@12: // Create one node in Hungarian and marked as private. Chris@12: $node = $this->drupalCreateNode([ Chris@12: 'body' => [[]], Chris@12: 'langcode' => 'hu', Chris@12: 'private' => [['value' => 1]], Chris@12: 'status' => 1, Chris@12: ]); Chris@12: Chris@12: // There should be one entry in node_access, with fallback set to hu. Chris@12: $this->checkRecords(1, 'hu'); Chris@12: Chris@12: // Create a translation user. Chris@12: $admin = $this->drupalCreateUser([ Chris@12: 'bypass node access', Chris@12: 'administer nodes', Chris@12: 'translate any entity', Chris@12: 'administer content translation', Chris@12: ]); Chris@12: $this->drupalLogin($admin); Chris@12: $this->drupalGet('node/' . $node->id() . '/translations'); Chris@12: $this->assertSession()->statusCodeEquals(200); Chris@12: Chris@12: // Create a Catalan translation through the UI. Chris@12: $url_options = ['language' => \Drupal::languageManager()->getLanguage('ca')]; Chris@12: $this->drupalGet('node/' . $node->id() . '/translations/add/hu/ca', $url_options); Chris@12: $this->assertSession()->statusCodeEquals(200); Chris@12: // Save the form. Chris@12: $this->getSession()->getPage()->pressButton('Save (this translation)'); Chris@12: $this->assertSession()->statusCodeEquals(200); Chris@12: Chris@12: // Check the node access table. Chris@12: $this->checkRecords(2, 'hu'); Chris@12: Chris@12: // Programmatically create a translation. This process lets us check that Chris@12: // both forms and code behave in the same way. Chris@12: $storage = \Drupal::entityTypeManager()->getStorage('node'); Chris@12: // Reload the node. Chris@12: $node = $storage->load(1); Chris@12: // Create an Afrikaans translation. Chris@12: $translation = $node->addTranslation('af'); Chris@12: $translation->title->value = $this->randomString(); Chris@12: $translation->status = 1; Chris@12: $node->save(); Chris@12: Chris@12: // Check the node access table. Chris@12: $this->checkRecords(3, 'hu'); Chris@12: Chris@12: // For completeness, edit the Catalan version again. Chris@12: $this->drupalGet('node/' . $node->id() . '/edit', $url_options); Chris@12: $this->assertSession()->statusCodeEquals(200); Chris@12: // Save the form. Chris@12: $this->getSession()->getPage()->pressButton('Save (this translation)'); Chris@12: $this->assertSession()->statusCodeEquals(200); Chris@12: // Check the node access table. Chris@12: $this->checkRecords(3, 'hu'); Chris@12: } Chris@12: Chris@12: /** Chris@12: * Queries the node_access table and checks for proper storage. Chris@12: * Chris@12: * @param int $count Chris@12: * The number of rows expected by the query (equal to the translation Chris@12: * count). Chris@12: * @param $langcode Chris@12: * The expected language code set as the fallback property. Chris@12: */ Chris@12: public function checkRecords($count, $langcode = 'hu') { Chris@12: $select = \Drupal::database() Chris@12: ->select('node_access', 'na') Chris@12: ->fields('na', ['nid', 'fallback', 'langcode', 'grant_view']) Chris@12: ->condition('na.realm', 'node_access_test', '=') Chris@12: ->condition('na.gid', 8888, '='); Chris@12: $records = $select->execute()->fetchAll(); Chris@12: // Check that the expected record count is returned. Chris@12: $this->assertEquals(count($records), $count); Chris@12: // The fallback value is 'hu' and should be set to 1. For other languages, Chris@12: // it should be set to 0. Casting to boolean lets us run that comparison. Chris@12: foreach ($records as $record) { Chris@12: $this->assertEquals((bool) $record->fallback, $record->langcode === $langcode); Chris@12: } Chris@12: } Chris@12: Chris@12: }