access.test 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Test class for access checks for VDE downloads.
  4. *
  5. * Views Data Export enforces that a previously exported file may only be
  6. * re-downloaded by the user that created the export. We test for that with
  7. * this class.
  8. */
  9. class ViewsDataExportAccessTest extends ViewsDataExportBaseTest {
  10. protected $profile = 'testing';
  11. public static function getInfo() {
  12. return array(
  13. 'name' => 'Access to temp files',
  14. 'description' => 'Check access to created export files.',
  15. 'group' => 'Views Data Export',
  16. );
  17. }
  18. /**
  19. * Test that VDE export can only be downloaded by the user that created them.
  20. */
  21. public function testExportedTempFileAccess() {
  22. $this->admin_user1 = $this->drupalCreateUser();
  23. $this->admin_user2 = $this->drupalCreateUser();
  24. // Run a batched export.
  25. $path = 'vde_test/' . $this->randomName();
  26. list($view, $expected) = $this->getExportView($path);
  27. $display = &$view->display['vde_test']->handler;
  28. // Set this view to be batched.
  29. $display->override_option('use_batch', 'batch');
  30. // Save this view so we can hit the path.
  31. $view->save();
  32. // Ensure that the menu router system is rebuilt on the next page load.
  33. variable_set('menu_rebuild_needed', TRUE);
  34. $this->drupalLogin($this->admin_user1);
  35. // Catpure the session_id as the redirects in the request ditch it.
  36. $session_id = $this->session_id;
  37. $this->assertBatchedExportEqual($path, $expected, 'Batched access export matched expected output.');
  38. // Remove all the test data, so future exports will be different.
  39. db_truncate('views_test')->execute();
  40. $this->resetAll();
  41. // Assert that we can re-download directly when supplying the token.
  42. // We rely on this being the first export in this test class.
  43. // Restore the session_id from above so we can use drupalGetToken.
  44. $this->session_id = $session_id;
  45. $token = $this->drupalGetToken('views_data_export/1');
  46. $this->drupalGet($path, array('query' => array('eid' => 1, 'download' => 1, 'token' => $token)));
  47. $output = $this->drupalGetContent();
  48. $this->assertEqual($this->normaliseString($output), $expected, 'Re-download of export file by original user is possible with session token.');
  49. // Assert that we cannot re-download directly without supplying the token.
  50. // We rely on this being the first export in this test class.
  51. $this->drupalGet($path, array('query' => array('eid' => 1, 'download' => 1)));
  52. $output = $this->drupalGetContent();
  53. $this->assertEqual($this->normaliseString($output), '', 'Re-download of export file by original user is not possible.');
  54. // Assert that someone else can't download our file.
  55. // We rely on this being the first export in this test class.
  56. $this->drupalLogin($this->admin_user2);
  57. $this->drupalGet($path, array('query' => array('eid' => 1, 'download' => 1, 'token' => $token)));
  58. $output = $this->drupalGetContent();
  59. $this->assertEqual($this->normaliseString($output), '', 'Re-download of export file by different user is not possible.');
  60. }
  61. /**
  62. * Overrides DrupalWebTestCase::drupalGetToken() to support the hash salt.
  63. *
  64. * @todo Remove when http://drupal.org/node/1555862 is fixed in core.
  65. */
  66. protected function drupalGetToken($value = '') {
  67. $private_key = drupal_get_private_key();
  68. return drupal_hmac_base64($value, $this->session_id . $private_key . drupal_get_hash_salt());
  69. }
  70. /**
  71. * Build and return a basic view of the views_test table.
  72. *
  73. * @return view
  74. */
  75. protected function getBasicExportView() {
  76. views_include('view');
  77. // Create the basic view.
  78. $view = new view();
  79. $view->vid = 'new';
  80. $view->base_table = 'views_test';
  81. // Set up the fields we need.
  82. $display = $view->new_display('default', 'Master', 'default');
  83. $display->override_option('fields', array(
  84. 'id' => array(
  85. 'id' => 'id',
  86. 'table' => 'views_test',
  87. 'field' => 'id',
  88. 'relationship' => 'none',
  89. ),
  90. 'name' => array(
  91. 'id' => 'name',
  92. 'table' => 'views_test',
  93. 'field' => 'name',
  94. 'relationship' => 'none',
  95. ),
  96. 'age' => array(
  97. 'id' => 'age',
  98. 'table' => 'views_test',
  99. 'field' => 'age',
  100. 'relationship' => 'none',
  101. ),
  102. ));
  103. // Set up the sort order.
  104. $display->override_option('sorts', array(
  105. 'id' => array(
  106. 'order' => 'ASC',
  107. 'id' => 'id',
  108. 'table' => 'views_test',
  109. 'field' => 'id',
  110. 'relationship' => 'none',
  111. ),
  112. ));
  113. // Set up the pager.
  114. $display->override_option('pager', array(
  115. 'type' => 'none',
  116. 'options' => array('offset' => 0),
  117. ));
  118. return $view;
  119. }
  120. protected function getStylePluginName() {
  121. return 'views_data_export_txt';
  122. }
  123. protected function getExportView($path = 'vde_test') {
  124. // Create the basic view.
  125. $view = $this->getBasicExportView();
  126. $display = $view->new_display('views_data_export', 'Data export', 'vde_test');
  127. $display->override_option('style_plugin', $this->getStylePluginName());
  128. $display->override_option('path', $path);
  129. $expected = '[ID]
  130. 1
  131. [Name]
  132. John
  133. [Age]
  134. 25
  135. ----------------------------------------
  136. [ID]
  137. 2
  138. [Name]
  139. George
  140. [Age]
  141. 27
  142. ----------------------------------------
  143. [ID]
  144. 3
  145. [Name]
  146. Ringo
  147. [Age]
  148. 28
  149. ----------------------------------------
  150. [ID]
  151. 4
  152. [Name]
  153. Paul
  154. [Age]
  155. 26
  156. ----------------------------------------
  157. [ID]
  158. 5
  159. [Name]
  160. Meredith
  161. [Age]
  162. 30
  163. ----------------------------------------';
  164. return array(&$view, $expected);
  165. }
  166. }