Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupalpermissions_by_termTests;
- use Drupalpermissions_by_termAccessCheckService;
- use Drupalpermissions_by_termAccessStorageService;
- use DrupalsimpletestWebTestBase;
- use DrupaltaxonomyEntityVocabulary;
- use DrupaltaxonomyEntityTerm;
- use DrupaluserEntityUser;
- use DrupalnodeEntityNode;
- use DrupalCoreLanguageLanguageInterface;
- use DrupalfieldEntityFieldStorageConfig;
- use DrupalfieldEntityFieldConfig;
- /**
- * Tests access restriction to nodes.
- *
- * Example command to run this test:
- * php core/scripts/run-tests.sh --url http://pbt-d8 --browser --verbose
- * --color --concurrency 4 --class
- * 'Drupalpermissions_by_termTestsRestrictedNodeTest'
- *
- * Make sure that the folder at /sites/simpletest is writable.
- *
- * @group permissions_by_term
- */
- class RestrictedNodeTest extends WebTestBase {
- /**
- * {@inheritdoc}
- */
- public static $modules = array('node', 'taxonomy', 'text', 'permissions_by_term');
- /**
- * List of taxonomy term names by language.
- *
- * @var array
- */
- public $term_names = [];
- /**
- * The vocabulary used for creating terms.
- *
- * @var DrupaltaxonomyVocabularyInterface
- */
- protected $vocabulary;
- protected $test_node_id;
- /**
- * Sets up the test.
- */
- protected function setUp() {
- parent::setUp();
- // Create a vocabulary.
- $this->vocabulary = Vocabulary::create([
- 'name' => $this->randomMachineName(),
- 'description' => $this->randomMachineName(),
- 'vid' => $this->randomMachineName(),
- 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
- 'help' => '',
- ]);
- $this->vocabulary->save();
- // Setup an authenticated user for our tests.
- $this->authenticated = User::create(array(
- 'name' => $this->randomMachineName(),
- 'mail' => 'non_admin@example.com',
- 'roles' => array(),
- 'pass' => user_password(),
- 'status' => 1,
- ));
- // @TODO: user is being saved, but no user id has been returned.
- $user_created = $this->authenticated->save();
- // Add a term to the vocabulary.
- $term = Term::create([
- 'name' => $this->randomMachineName(),
- 'description' => $this->randomMachineName(),
- 'vid' => $this->vocabulary->id(),
- 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
- ]);
- $term->save();
- $oDbConnection = Drupal::database();
- $access_storage_service = new AccessStorageService($oDbConnection, NULL, $term->id());
- // @TODO: remove 3 and add real user id.
- $access_storage_service->addTermPermissionsByUserIds([4]);
- FieldStorageConfig::create([
- 'entity_type' => 'node',
- 'field_name' => 'field_tags',
- 'type' => 'entity_reference',
- 'settings' => [
- 'target_type' => 'taxonomy_term',
- ],
- ])->save();
- $this->drupalCreateContentType(['type' => 'article']);
- FieldConfig::create([
- 'field_name' => 'field_tags',
- 'entity_type' => 'node',
- 'bundle' => 'article',
- ])->save();
- $node = Node::create([
- 'type' => 'article',
- 'title' => 'test node',
- 'field_tags' => [$term->id()],
- ]);
- $node_save_ret = $node->save();
- $this->assertTrue($node_save_ret, 'Node could be saved.');
- $this->test_node_id = $node->id();
- $account = $this->drupalCreateUser();
- $this->drupalLogin($account);
- $access_check_service = new AccessCheckService();
- $ret = $access_check_service->canUserAccessByNodeId(1);
- // Visit a Drupal page that requires login.
- $drupal_get_ret = $this->drupalGet('node/1');
- $this->assertText('Access denied');
- }
- public function testAccess() {
- //$this->assertResponse(200, 'Page was accessible');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement