Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupalpermissions_by_termTests;
  4.  
  5. use Drupalpermissions_by_termAccessCheckService;
  6. use Drupalpermissions_by_termAccessStorageService;
  7. use DrupalsimpletestWebTestBase;
  8. use DrupaltaxonomyEntityVocabulary;
  9. use DrupaltaxonomyEntityTerm;
  10. use DrupaluserEntityUser;
  11. use DrupalnodeEntityNode;
  12. use DrupalCoreLanguageLanguageInterface;
  13. use DrupalfieldEntityFieldStorageConfig;
  14. use DrupalfieldEntityFieldConfig;
  15.  
  16. /**
  17. * Tests access restriction to nodes.
  18. *
  19. * Example command to run this test:
  20. * php core/scripts/run-tests.sh --url http://pbt-d8 --browser --verbose
  21. * --color --concurrency 4 --class
  22. * 'Drupalpermissions_by_termTestsRestrictedNodeTest'
  23. *
  24. * Make sure that the folder at /sites/simpletest is writable.
  25. *
  26. * @group permissions_by_term
  27. */
  28. class RestrictedNodeTest extends WebTestBase {
  29.  
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public static $modules = array('node', 'taxonomy', 'text', 'permissions_by_term');
  34.  
  35. /**
  36. * List of taxonomy term names by language.
  37. *
  38. * @var array
  39. */
  40. public $term_names = [];
  41.  
  42. /**
  43. * The vocabulary used for creating terms.
  44. *
  45. * @var DrupaltaxonomyVocabularyInterface
  46. */
  47. protected $vocabulary;
  48.  
  49. protected $test_node_id;
  50.  
  51. /**
  52. * Sets up the test.
  53. */
  54. protected function setUp() {
  55. parent::setUp();
  56.  
  57. // Create a vocabulary.
  58. $this->vocabulary = Vocabulary::create([
  59. 'name' => $this->randomMachineName(),
  60. 'description' => $this->randomMachineName(),
  61. 'vid' => $this->randomMachineName(),
  62. 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  63. 'help' => '',
  64. ]);
  65. $this->vocabulary->save();
  66.  
  67. // Setup an authenticated user for our tests.
  68. $this->authenticated = User::create(array(
  69. 'name' => $this->randomMachineName(),
  70. 'mail' => 'non_admin@example.com',
  71. 'roles' => array(),
  72. 'pass' => user_password(),
  73. 'status' => 1,
  74. ));
  75. // @TODO: user is being saved, but no user id has been returned.
  76. $user_created = $this->authenticated->save();
  77.  
  78. // Add a term to the vocabulary.
  79. $term = Term::create([
  80. 'name' => $this->randomMachineName(),
  81. 'description' => $this->randomMachineName(),
  82. 'vid' => $this->vocabulary->id(),
  83. 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  84. ]);
  85. $term->save();
  86.  
  87. $oDbConnection = Drupal::database();
  88. $access_storage_service = new AccessStorageService($oDbConnection, NULL, $term->id());
  89. // @TODO: remove 3 and add real user id.
  90. $access_storage_service->addTermPermissionsByUserIds([4]);
  91.  
  92. FieldStorageConfig::create([
  93. 'entity_type' => 'node',
  94. 'field_name' => 'field_tags',
  95. 'type' => 'entity_reference',
  96. 'settings' => [
  97. 'target_type' => 'taxonomy_term',
  98. ],
  99. ])->save();
  100.  
  101. $this->drupalCreateContentType(['type' => 'article']);
  102. FieldConfig::create([
  103. 'field_name' => 'field_tags',
  104. 'entity_type' => 'node',
  105. 'bundle' => 'article',
  106. ])->save();
  107.  
  108. $node = Node::create([
  109. 'type' => 'article',
  110. 'title' => 'test node',
  111. 'field_tags' => [$term->id()],
  112. ]);
  113.  
  114. $node_save_ret = $node->save();
  115.  
  116. $this->assertTrue($node_save_ret, 'Node could be saved.');
  117.  
  118. $this->test_node_id = $node->id();
  119.  
  120. $account = $this->drupalCreateUser();
  121. $this->drupalLogin($account);
  122.  
  123. $access_check_service = new AccessCheckService();
  124. $ret = $access_check_service->canUserAccessByNodeId(1);
  125.  
  126. // Visit a Drupal page that requires login.
  127. $drupal_get_ret = $this->drupalGet('node/1');
  128.  
  129. $this->assertText('Access denied');
  130.  
  131. }
  132.  
  133. public function testAccess() {
  134.  
  135.  
  136. //$this->assertResponse(200, 'Page was accessible');
  137. }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement