draggableviews.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /**
  3. * @file
  4. * Test cases file.
  5. */
  6. /**
  7. * Class for testing Draggableviews module.
  8. */
  9. class DraggableviewsTestCase extends DrupalWebTestCase {
  10. function setUp() {
  11. parent::setUp(array('ctools', 'views', 'views_ui', 'entity', 'draggableviews', 'draggableviews_test'));
  12. }
  13. /**
  14. * Fork from drupalPost().
  15. *
  16. * When action of the form determined we don't care about exposed filter
  17. * arguments passed to the view. In this fork we use
  18. * $this->getUrl() unconditionally.
  19. */
  20. protected function drupalDraggableviewsPost($path, $edit, $submit, array $options = array(), array $headers = array(), $form_html_id = NULL, $extra_post = NULL) {
  21. $submit_matches = FALSE;
  22. $ajax = is_array($submit);
  23. if (isset($path)) {
  24. $this->drupalGet($path, $options);
  25. }
  26. if ($this->parse()) {
  27. $edit_save = $edit;
  28. // Let's iterate over all the forms.
  29. $xpath = "//form";
  30. if (!empty($form_html_id)) {
  31. $xpath .= "[@id='" . $form_html_id . "']";
  32. }
  33. $forms = $this->xpath($xpath);
  34. foreach ($forms as $form) {
  35. // We try to set the fields of this form as specified in $edit.
  36. $edit = $edit_save;
  37. $post = array();
  38. $upload = array();
  39. $submit_matches = $this->handleForm($post, $edit, $upload, $ajax ? NULL : $submit, $form);
  40. // $action = isset($form['action']) ? $this->getAbsoluteUrl((string) $form['action']) : $this->getUrl();
  41. $action = $this->getUrl();
  42. if ($ajax) {
  43. $action = $this->getAbsoluteUrl(!empty($submit['path']) ? $submit['path'] : 'system/ajax');
  44. // Ajax callbacks verify the triggering element if necessary, so while
  45. // we may eventually want extra code that verifies it in the
  46. // handleForm() function, it's not currently a requirement.
  47. $submit_matches = TRUE;
  48. }
  49. // We post only if we managed to handle every field in edit and the
  50. // submit button matches.
  51. if (!$edit && ($submit_matches || !isset($submit))) {
  52. $post_array = $post;
  53. if ($upload) {
  54. // TODO: cURL handles file uploads for us, but the implementation
  55. // is broken. This is a less than elegant workaround. Alternatives
  56. // are being explored at #253506.
  57. foreach ($upload as $key => $file) {
  58. $file = drupal_realpath($file);
  59. if ($file && is_file($file)) {
  60. $post[$key] = '@' . $file;
  61. }
  62. }
  63. }
  64. else {
  65. foreach ($post as $key => $value) {
  66. // Encode according to application/x-www-form-urlencoded
  67. // Both names and values needs to be urlencoded, according to
  68. // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
  69. $post[$key] = urlencode($key) . '=' . urlencode($value);
  70. }
  71. $post = implode('&', $post) . $extra_post;
  72. }
  73. $out = $this->curlExec(array(CURLOPT_URL => $action, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $post, CURLOPT_HTTPHEADER => $headers));
  74. // Ensure that any changes to variables in the other thread are picked up.
  75. $this->refreshVariables();
  76. // Replace original page output with new output from redirected page(s).
  77. if ($new = $this->checkForMetaRefresh()) {
  78. $out = $new;
  79. }
  80. $this->verbose('POST request to: ' . $path .
  81. '<hr />Ending URL: ' . $this->getUrl() .
  82. '<hr />Fields: ' . highlight_string('<?php ' . var_export($post_array, TRUE), TRUE) .
  83. '<hr />' . $out);
  84. return $out;
  85. }
  86. }
  87. // We have not found a form which contained all fields of $edit.
  88. foreach ($edit as $name => $value) {
  89. $this->fail(t('Failed to set field @name to @value', array('@name' => $name, '@value' => $value)));
  90. }
  91. if (!$ajax && isset($submit)) {
  92. $this->assertTrue($submit_matches, t('Found the @submit button', array('@submit' => $submit)));
  93. }
  94. $this->fail(t('Found the requested form fields at @path', array('@path' => $path)));
  95. }
  96. }
  97. }
  98. /**
  99. * Testing Native Handler.
  100. */
  101. class DraggableviewsNativeHandlerTestCase extends DraggableviewsTestCase {
  102. public static function getInfo() {
  103. return array(
  104. 'name' => 'Native handler',
  105. 'description' => 'Test the native handler.',
  106. 'group' => 'Draggableviews',
  107. );
  108. }
  109. function testSort() {
  110. $permissions = array('access content');
  111. $rid = $this->drupalCreateRole($permissions);
  112. // Create five test users.
  113. $accounts = array();
  114. for ($i = 0; $i < 5; $i++) {
  115. $edit = array();
  116. $edit['name'] = $this->randomName();
  117. // First three users should be prefixed 'test_'.
  118. if ($i < 3) {
  119. $edit['name'] = 'test_' . $edit['name'];
  120. }
  121. $edit['mail'] = $edit['name'] . '@example.com';
  122. $edit['roles'] = array($rid => $rid);
  123. $edit['pass'] = user_password();
  124. $edit['status'] = 1;
  125. $account = user_save(drupal_anonymous_user(), $edit);
  126. $account->pass_raw = $edit['pass'];
  127. $accounts[$account->uid] = $account;
  128. }
  129. $account = $this->drupalCreateUser(array('access content', 'access draggableviews', 'access user profiles', 'access contextual links'));
  130. $this->drupalLogin($account);
  131. // Now lets sort and save a view.
  132. $edit = array(
  133. 'draggableviews[0][weight]' => 0,
  134. 'draggableviews[0][id]' => 1,
  135. 'draggableviews[1][weight]' => 1,
  136. 'draggableviews[1][id]' => 2,
  137. 'draggableviews[2][weight]' => 2,
  138. 'draggableviews[2][id]' => 3,
  139. 'draggableviews[3][weight]' => 3,
  140. 'draggableviews[3][id]' => 4,
  141. 'draggableviews[4][weight]' => 4,
  142. 'draggableviews[4][id]' => 5,
  143. 'draggableviews[5][weight]' => 5,
  144. 'draggableviews[5][id]' => 6,
  145. 'draggableviews[6][weight]' => 6,
  146. 'draggableviews[6][id]' => 7,
  147. );
  148. $this->drupalPost('users-set', $edit, t('Save'));
  149. // Assert that first user is on first place, and second is on second.
  150. $first_row = $this->xpath('//tr[@class="odd views-row-first draggable"]/td/a[@class="username"]');
  151. $second_row = $this->xpath('//tr[@class="even draggable"]/td/a[@class="username"]');
  152. $this->assertEqual((string) $first_row[0], 'placeholder-for...', t('First row user uid 1.'));
  153. $this->assertEqual((string) $second_row[0], $accounts[2]->name, t('Second row user uid 2.'));
  154. // Now save a different sort (first and second rows changed places).
  155. $edit = array(
  156. 'draggableviews[0][weight]' => 0,
  157. 'draggableviews[0][id]' => 2,
  158. 'draggableviews[1][weight]' => 1,
  159. 'draggableviews[1][id]' => 1,
  160. 'draggableviews[2][weight]' => 2,
  161. 'draggableviews[2][id]' => 3,
  162. 'draggableviews[3][weight]' => 3,
  163. 'draggableviews[3][id]' => 4,
  164. 'draggableviews[4][weight]' => 4,
  165. 'draggableviews[4][id]' => 5,
  166. 'draggableviews[5][weight]' => 5,
  167. 'draggableviews[5][id]' => 6,
  168. 'draggableviews[6][weight]' => 6,
  169. 'draggableviews[6][id]' => 7,
  170. );
  171. $this->drupalPost('users-set', $edit, t('Save'));
  172. // Assert that first user is on second place, and second user is on first.
  173. $first_row = $this->xpath('//tr[@class="odd views-row-first draggable"]/td/a[@class="username"]');
  174. $second_row = $this->xpath('//tr[@class="even draggable"]/td/a[@class="username"]');
  175. $this->assertEqual((string) $first_row[0], $accounts[2]->name, t('First row user uid 2.'));
  176. $this->assertEqual((string) $second_row[0], 'placeholder-for...', t('Second row user uid 1.'));
  177. // Apply exposed filter and set weights.
  178. $edit = array(
  179. 'draggableviews[0][weight]' => 0,
  180. 'draggableviews[0][id]' => 4,
  181. 'draggableviews[1][weight]' => 1,
  182. 'draggableviews[1][id]' => 3,
  183. 'draggableviews[2][weight]' => 2,
  184. 'draggableviews[2][id]' => 2,
  185. );
  186. $this->drupalDraggableviewsPost('users-set', $edit, t('Save'), array('query' => array('mail' => 'test')));
  187. // Now lets check display view page.
  188. $this->drupalGet('users-display');
  189. $first_row = $this->xpath('//tr[@class="odd views-row-first"]/td/a[@class="username"]');
  190. $second_row = $this->xpath('//tr[@class="even"]/td/a[@class="username"]');
  191. $this->assertEqual((string) $first_row[0], $accounts[2]->name, t('Display view. First row user uid 2.'));
  192. $this->assertEqual((string) $second_row[0], 'placeholder-for...', t('Display view. Second row user uid 1.'));
  193. // Check display view with applied exposed filter.
  194. $this->drupalGet('users-display', array('query' => array('mail' => 'test')));
  195. $first_row = $this->xpath('//tr[@class="odd views-row-first"]/td/a[@class="username"]');
  196. $second_row = $this->xpath('//tr[@class="even"]/td/a[@class="username"]');
  197. $this->assertEqual((string) $first_row[0], $accounts[4]->name, t('Display view. Exposed filter applied. First row user uid 4.'));
  198. $this->assertEqual((string) $second_row[0], $accounts[3]->name, t('Display view. Exposed filter applied. Second row user uid 3.'));
  199. // Check contextual link existense.
  200. $contextual_links = $this->xpath('//ul[@class="contextual-links views-contextual-links-page"]/li/a');
  201. $href = (string) $contextual_links[0]['href'];
  202. $this->assertTrue(strpos($href, 'users-set?destination=users-display') !== FALSE, t('Contextual link exists.'));
  203. }
  204. }
  205. /**
  206. * Testing Fielad API Handler.
  207. */
  208. class DraggableviewsFieldAPIHandlerTestCase extends DraggableviewsTestCase {
  209. public static function getInfo() {
  210. return array(
  211. 'name' => 'Field API handler',
  212. 'description' => 'Test the Field API handler.',
  213. 'group' => 'Draggableviews',
  214. );
  215. }
  216. public function testSort() {
  217. $this->createField();
  218. $account = $this->drupalCreateUser(array('access content', 'access draggableviews', 'access user profiles', 'access contextual links'));
  219. $this->drupalLogin($account);
  220. // Create five nodes.
  221. $nodes = array();
  222. for ($i = 0; $i < 5; $i++) {
  223. $node = $this->drupalCreateNode(array('type' => 'article',));
  224. $nodes[$node->nid] = $node;
  225. }
  226. // Now lets sort and save a view.
  227. $edit = array(
  228. 'draggableviews[0][weight]' => 0,
  229. 'draggableviews[0][id]' => 1,
  230. 'draggableviews[1][weight]' => 1,
  231. 'draggableviews[1][id]' => 2,
  232. 'draggableviews[2][weight]' => 2,
  233. 'draggableviews[2][id]' => 3,
  234. 'draggableviews[3][weight]' => 3,
  235. 'draggableviews[3][id]' => 4,
  236. 'draggableviews[4][weight]' => 4,
  237. 'draggableviews[4][id]' => 5,
  238. );
  239. $this->drupalPost('nodes-set', $edit, t('Save'));
  240. // Assert that first node is on first place, and second is on second.
  241. $first_row = $this->xpath('//tr[@class="odd views-row-first draggable"]/td/a');
  242. $second_row = $this->xpath('//tr[@class="even draggable"]/td/a');
  243. $this->assertEqual((string) $first_row[0], $nodes[1]->title, t('First row node nid 1.'));
  244. $this->assertEqual((string) $second_row[0], $nodes[2]->title, t('Second row node nid 2.'));
  245. // Now save a different sort (first and second rows changed places).
  246. $edit = array(
  247. 'draggableviews[0][weight]' => 0,
  248. 'draggableviews[0][id]' => 2,
  249. 'draggableviews[1][weight]' => 1,
  250. 'draggableviews[1][id]' => 1,
  251. 'draggableviews[2][weight]' => 2,
  252. 'draggableviews[2][id]' => 3,
  253. 'draggableviews[3][weight]' => 3,
  254. 'draggableviews[3][id]' => 4,
  255. 'draggableviews[4][weight]' => 4,
  256. 'draggableviews[4][id]' => 5,
  257. );
  258. $this->drupalPost('nodes-set', $edit, t('Save'));
  259. // Assert that first node is on second place, and second is on first.
  260. $first_row = $this->xpath('//tr[@class="odd views-row-first draggable"]/td/a');
  261. $second_row = $this->xpath('//tr[@class="even draggable"]/td/a');
  262. $this->assertEqual((string) $first_row[0], $nodes[2]->title, t('First row node nid 2.'));
  263. $this->assertEqual((string) $second_row[0], $nodes[1]->title, t('Second row node nid 1.'));
  264. // Check display view order.
  265. $this->drupalGet('nodes-display');
  266. $first_row = $this->xpath('//tr[@class="odd views-row-first"]/td/a');
  267. $second_row = $this->xpath('//tr[@class="even"]/td/a');
  268. $this->assertEqual((string) $first_row[0], $nodes[2]->title, t('First row node nid 2.'));
  269. $this->assertEqual((string) $second_row[0], $nodes[1]->title, t('Second row node nid 1.'));
  270. // Check values of nodes.
  271. $node1 = node_load(1);
  272. $node2 = node_load(2);
  273. $this->assertTrue($node1->field_weight[LANGUAGE_NONE][0]['value'] > $node2->field_weight[LANGUAGE_NONE][0]['value'], t('Weight of node 1 is more than weight of node 2.'));
  274. }
  275. // Create a integer field for Article nodes.
  276. function createField() {
  277. $field = array (
  278. 'translatable' => '0',
  279. 'entity_types' =>
  280. array (
  281. ),
  282. 'settings' =>
  283. array (
  284. ),
  285. 'storage' =>
  286. array (
  287. 'type' => 'field_sql_storage',
  288. 'settings' =>
  289. array (
  290. ),
  291. 'module' => 'field_sql_storage',
  292. 'active' => '1',
  293. 'details' =>
  294. array (
  295. 'sql' =>
  296. array (
  297. 'FIELD_LOAD_CURRENT' =>
  298. array (
  299. 'field_data_field_weight' =>
  300. array (
  301. 'value' => 'field_weight_value',
  302. ),
  303. ),
  304. 'FIELD_LOAD_REVISION' =>
  305. array (
  306. 'field_revision_field_weight' =>
  307. array (
  308. 'value' => 'field_weight_value',
  309. ),
  310. ),
  311. ),
  312. ),
  313. ),
  314. 'foreign keys' =>
  315. array (
  316. ),
  317. 'indexes' =>
  318. array (
  319. ),
  320. 'id' => '5',
  321. 'field_name' => 'field_weight',
  322. 'type' => 'number_integer',
  323. 'module' => 'number',
  324. 'active' => '1',
  325. 'locked' => '0',
  326. 'cardinality' => '1',
  327. 'deleted' => '0',
  328. 'columns' =>
  329. array (
  330. 'value' =>
  331. array (
  332. 'type' => 'int',
  333. 'not null' => false,
  334. ),
  335. ),
  336. 'bundles' =>
  337. array (
  338. 'node' =>
  339. array (
  340. 0 => 'article',
  341. ),
  342. ),
  343. );
  344. field_create_field($field);
  345. $instance = array (
  346. 'label' => 'Weight',
  347. 'widget' =>
  348. array (
  349. 'weight' => 0,
  350. 'type' => 'number',
  351. 'module' => 'number',
  352. 'active' => 0,
  353. 'settings' =>
  354. array (
  355. ),
  356. ),
  357. 'settings' =>
  358. array (
  359. 'min' => '',
  360. 'max' => '',
  361. 'prefix' => '',
  362. 'suffix' => '',
  363. 'user_register_form' => false,
  364. ),
  365. 'display' =>
  366. array (
  367. 'default' =>
  368. array (
  369. 'label' => 'above',
  370. 'type' => 'number_integer',
  371. 'settings' =>
  372. array (
  373. 'thousand_separator' => ' ',
  374. 'decimal_separator' => '.',
  375. 'scale' => 0,
  376. 'prefix_suffix' => true,
  377. ),
  378. 'module' => 'number',
  379. 'weight' => 11,
  380. ),
  381. ),
  382. 'required' => 0,
  383. 'description' => '',
  384. 'default_value' => NULL,
  385. 'id' => '7',
  386. 'field_id' => '5',
  387. 'field_name' => 'field_weight',
  388. 'entity_type' => 'node',
  389. 'bundle' => 'article',
  390. 'deleted' => '0',
  391. );
  392. field_create_instance($instance);
  393. }
  394. }