views_ui.test 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. <?php
  2. /**
  3. * @file
  4. * Tests Views UI Wizard.
  5. */
  6. /**
  7. * Views UI wizard tests.
  8. */
  9. class ViewsUIWizardHelper extends DrupalWebTestCase {
  10. function setUp() {
  11. // Enable views_ui.
  12. parent::setUp('views_ui');
  13. // Create and log in a user with administer views permission.
  14. $views_admin = $this->drupalCreateUser(array('administer views', 'administer blocks', 'bypass node access', 'access user profiles', 'view revisions'));
  15. $this->drupalLogin($views_admin);
  16. }
  17. }
  18. /**
  19. * Tests creating views with the wizard and viewing them on the listing page.
  20. */
  21. class ViewsUIWizardBasicTestCase extends ViewsUIWizardHelper {
  22. public static function getInfo() {
  23. return array(
  24. 'name' => 'Views UI wizard basic functionality',
  25. 'description' => 'Test creating basic views with the wizard and viewing them on the listing page.',
  26. 'group' => 'Views UI',
  27. );
  28. }
  29. function testViewsWizardAndListing() {
  30. // Check if we can access the main views admin page.
  31. $this->drupalGet('admin/structure/views');
  32. $this->assertText(t('Add new view'));
  33. // Create a simple and not at all useful view.
  34. $view1 = array();
  35. $view1['human_name'] = $this->randomName(16);
  36. $view1['name'] = strtolower($this->randomName(16));
  37. $view1['description'] = $this->randomName(16);
  38. $view1['page[create]'] = FALSE;
  39. $this->drupalPost('admin/structure/views/add', $view1, t('Save & exit'));
  40. $this->assertText(t('Your view was saved. You may edit it from the list below.'));
  41. $this->assertText($view1['human_name']);
  42. $this->assertText($view1['description']);
  43. foreach(array('delete', 'clone', 'edit') as $operation) {
  44. $this->assertLinkByHref(url('admin/structure/views/view/' . $view1['name'] . '/' . $operation));
  45. }
  46. // This view should not have a block.
  47. $this->drupalGet('admin/structure/block');
  48. $this->assertNoText('View: ' . $view1['human_name']);
  49. // Create two nodes.
  50. $node1 = $this->drupalCreateNode(array('type' => 'page'));
  51. $node2 = $this->drupalCreateNode(array('type' => 'article'));
  52. // Now create a page with simple node listing and an attached feed.
  53. $view2 = array();
  54. $view2['human_name'] = $this->randomName(16);
  55. $view2['name'] = strtolower($this->randomName(16));
  56. $view2['description'] = $this->randomName(16);
  57. $view2['page[create]'] = 1;
  58. $view2['page[title]'] = $this->randomName(16);
  59. $view2['page[path]'] = $this->randomName(16);
  60. $view2['page[feed]'] = 1;
  61. $view2['page[feed_properties][path]'] = $this->randomName(16);
  62. $this->drupalPost('admin/structure/views/add', $view2, t('Save & exit'));
  63. // Since the view has a page, we expect to be automatically redirected to
  64. // it.
  65. $this->assertUrl($view2['page[path]']);
  66. $this->assertText($view2['page[title]']);
  67. $this->assertText($node1->title);
  68. $this->assertText($node2->title);
  69. // Check if we have the feed.
  70. $this->assertLinkByHref(url($view2['page[feed_properties][path]']));
  71. $this->drupalGet($view2['page[feed_properties][path]']);
  72. $this->assertRaw('<rss version="2.0"');
  73. // The feed should have the same title and nodes as the page.
  74. $this->assertText($view2['page[title]']);
  75. $this->assertRaw(url('node/' . $node1->nid, array('absolute' => TRUE)));
  76. $this->assertText($node1->title);
  77. $this->assertRaw(url('node/' . $node2->nid, array('absolute' => TRUE)));
  78. $this->assertText($node2->title);
  79. // Go back to the views page and check if this view is there.
  80. $this->drupalGet('admin/structure/views');
  81. $this->assertText($view2['human_name']);
  82. $this->assertText($view2['description']);
  83. $this->assertLinkByHref(url($view2['page[path]']));
  84. // This view should not have a block.
  85. $this->drupalGet('admin/structure/block');
  86. $this->assertNoText('View: ' . $view2['human_name']);
  87. // Create a view with a page and a block, and filter the listing.
  88. $view3 = array();
  89. $view3['human_name'] = $this->randomName(16);
  90. $view3['name'] = strtolower($this->randomName(16));
  91. $view3['description'] = $this->randomName(16);
  92. $view3['show[wizard_key]'] = 'node';
  93. $view3['show[type]'] = 'page';
  94. $view3['page[create]'] = 1;
  95. $view3['page[title]'] = $this->randomName(16);
  96. $view3['page[path]'] = $this->randomName(16);
  97. $view3['block[create]'] = 1;
  98. $view3['block[title]'] = $this->randomName(16);
  99. $this->drupalPost('admin/structure/views/add', $view3, t('Save & exit'));
  100. // Make sure the view only displays the node we expect.
  101. $this->assertUrl($view3['page[path]']);
  102. $this->assertText($view3['page[title]']);
  103. $this->assertText($node1->title);
  104. $this->assertNoText($node2->title);
  105. // Go back to the views page and check if this view is there.
  106. $this->drupalGet('admin/structure/views');
  107. $this->assertText($view3['human_name']);
  108. $this->assertText($view3['description']);
  109. $this->assertLinkByHref(url($view3['page[path]']));
  110. // Put the block into the first sidebar region.
  111. $this->drupalGet('admin/structure/block');
  112. $this->assertText('View: ' . $view3['human_name']);
  113. $edit = array();
  114. $edit["blocks[views_{$view3['name']}-block][region]"] = 'sidebar_first';
  115. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  116. // Visit a random page (not the one that displays the view itself) and look
  117. // for the expected node title in the block.
  118. $this->drupalGet('user');
  119. $this->assertText($node1->title);
  120. $this->assertNoText($node2->title);
  121. // Check if the export screen works.
  122. $this->drupalGet('admin/structure/views/view/' . $view3['name'] . '/export');
  123. $this->assertRaw('$view = new view();');
  124. $this->assertRaw($view3['human_name']);
  125. $this->assertRaw($view3['description']);
  126. // Make sure the listing page doesn't show disabled default views.
  127. $this->assertNoText('tracker', t('Default tracker view does not show on the listing page.'));
  128. }
  129. }
  130. /**
  131. * Tests enabling, disabling, and reverting default views via the listing page.
  132. */
  133. class ViewsUIWizardDefaultViewsTestCase extends ViewsUIWizardHelper {
  134. public static function getInfo() {
  135. return array(
  136. 'name' => 'Views UI default views functionality',
  137. 'description' => 'Test enabling, disabling, and reverting default views via the listing page.',
  138. 'group' => 'Views UI',
  139. );
  140. }
  141. /**
  142. * Tests default views.
  143. */
  144. function testDefaultViews() {
  145. // Make sure the front page view starts off as disabled (does not appear on
  146. // the listing page).
  147. $edit_href = 'admin/structure/views/view/frontpage/edit';
  148. $this->drupalGet('admin/structure/views');
  149. // TODO: Disabled default views do now appear on the front page. Test this
  150. // behavior with templates instead.
  151. // $this->assertNoLinkByHref($edit_href);
  152. // Enable the front page view, and make sure it is now visible on the main
  153. // listing page.
  154. $this->drupalGet('admin/structure/views/templates');
  155. $this->clickViewsOperationLink(t('Enable'), '/frontpage/');
  156. $this->assertUrl('admin/structure/views');
  157. $this->assertLinkByHref($edit_href);
  158. // It should not be possible to revert the view yet.
  159. $this->assertNoLink(t('Revert'));
  160. $revert_href = 'admin/structure/views/view/frontpage/revert';
  161. $this->assertNoLinkByHref($revert_href);
  162. // Edit the view and change the title. Make sure that the new title is
  163. // displayed.
  164. $new_title = $this->randomName(16);
  165. $edit = array('title' => $new_title);
  166. $this->drupalPost('admin/structure/views/nojs/display/frontpage/page/title', $edit, t('Apply'));
  167. $this->drupalPost('admin/structure/views/view/frontpage/edit/page', array(), t('Save'));
  168. $this->drupalGet('frontpage');
  169. $this->assertText($new_title);
  170. // It should now be possible to revert the view. Do that, and make sure the
  171. // view title we added above no longer is displayed.
  172. $this->drupalGet('admin/structure/views');
  173. $this->assertLink(t('Revert'));
  174. $this->assertLinkByHref($revert_href);
  175. $this->drupalPost($revert_href, array(), t('Revert'));
  176. $this->drupalGet('frontpage');
  177. $this->assertNoText($new_title);
  178. // Now disable the view, and make sure it stops appearing on the main view
  179. // listing page but instead goes back to displaying on the disabled views
  180. // listing page.
  181. // TODO: Test this behavior with templates instead.
  182. $this->drupalGet('admin/structure/views');
  183. $this->clickViewsOperationLink(t('Disable'), '/frontpage/');
  184. // $this->assertUrl('admin/structure/views');
  185. // $this->assertNoLinkByHref($edit_href);
  186. // The easiest way to verify it appears on the disabled views listing page
  187. // is to try to click the "enable" link from there again.
  188. $this->drupalGet('admin/structure/views/templates');
  189. $this->clickViewsOperationLink(t('Enable'), '/frontpage/');
  190. $this->assertUrl('admin/structure/views');
  191. $this->assertLinkByHref($edit_href);
  192. }
  193. /**
  194. * Click a link to perform an operation on a view.
  195. *
  196. * In general, we expect lots of links titled "enable" or "disable" on the
  197. * various views listing pages, and they might have tokens in them. So we
  198. * need special code to find the correct one to click.
  199. *
  200. * @param $label
  201. * Text between the anchor tags of the desired link.
  202. * @param $unique_href_part
  203. * A unique string that is expected to occur within the href of the desired
  204. * link. For example, if the link URL is expected to look like
  205. * "admin/structure/views/view/frontpage/...", then "/frontpage/" could be
  206. * passed as the expected unique string.
  207. *
  208. * @return
  209. * The page content that results from clicking on the link, or FALSE on
  210. * failure. Failure also results in a failed assertion.
  211. */
  212. function clickViewsOperationLink($label, $unique_href_part) {
  213. $links = $this->xpath('//a[normalize-space(text())=:label]', array(':label' => $label));
  214. foreach ($links as $link_index => $link) {
  215. $position = strpos($link['href'], $unique_href_part);
  216. if ($position !== FALSE) {
  217. $index = $link_index;
  218. break;
  219. }
  220. }
  221. $this->assertTrue(isset($index), t('Link to "@label" containing @part found.', array('@label' => $label, '@part' => $unique_href_part)));
  222. if (isset($index)) {
  223. return $this->clickLink($label, $index);
  224. }
  225. else {
  226. return FALSE;
  227. }
  228. }
  229. }
  230. /**
  231. * Tests the ability of the views wizard to create views filtered by taxonomy.
  232. */
  233. class ViewsUIWizardTaggedWithTestCase extends ViewsUIWizardHelper {
  234. protected $node_type_with_tags;
  235. protected $node_type_without_tags;
  236. protected $tag_vocabulary;
  237. protected $tag_field;
  238. protected $tag_instance;
  239. public static function getInfo() {
  240. return array(
  241. 'name' => 'Views UI wizard taxonomy functionality',
  242. 'description' => 'Test the ability of the views wizard to create views filtered by taxonomy.',
  243. 'group' => 'Views UI',
  244. );
  245. }
  246. function setUp() {
  247. parent::setUp();
  248. // Create two content types. One will have an autocomplete tagging field,
  249. // and one won't.
  250. $this->node_type_with_tags = $this->drupalCreateContentType();
  251. $this->node_type_without_tags = $this->drupalCreateContentType();
  252. // Create the vocabulary for the tag field.
  253. $this->tag_vocabulary = new stdClass();
  254. $this->tag_vocabulary->name = 'Views testing tags';
  255. $this->tag_vocabulary->machine_name = 'views_testing_tags';
  256. taxonomy_vocabulary_save($this->tag_vocabulary);
  257. // Create the tag field itself.
  258. $this->tag_field = array(
  259. 'field_name' => 'field_views_testing_tags',
  260. 'type' => 'taxonomy_term_reference',
  261. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  262. 'settings' => array(
  263. 'allowed_values' => array(
  264. array(
  265. 'vocabulary' => $this->tag_vocabulary->machine_name,
  266. 'parent' => 0,
  267. ),
  268. ),
  269. ),
  270. );
  271. field_create_field($this->tag_field);
  272. // Create an instance of the tag field on one of the content types, and
  273. // configure it to display an autocomplete widget.
  274. $this->tag_instance = array(
  275. 'field_name' => 'field_views_testing_tags',
  276. 'entity_type' => 'node',
  277. 'bundle' => $this->node_type_with_tags->type,
  278. 'widget' => array(
  279. 'type' => 'taxonomy_autocomplete',
  280. ),
  281. 'display' => array(
  282. 'default' => array(
  283. 'type' => 'taxonomy_term_reference_link',
  284. 'weight' => 10,
  285. ),
  286. 'teaser' => array(
  287. 'type' => 'taxonomy_term_reference_link',
  288. 'weight' => 10,
  289. ),
  290. ),
  291. );
  292. field_create_instance($this->tag_instance);
  293. }
  294. /**
  295. * Tests the "tagged with" functionality.
  296. */
  297. function testTaggedWith() {
  298. // In this test we will only create nodes that have an instance of the tag
  299. // field.
  300. $node_add_path = 'node/add/' . $this->node_type_with_tags->type;
  301. // Create three nodes, with different tags.
  302. $tag_field = $this->tag_field['field_name'] . '[' . LANGUAGE_NONE . ']';
  303. $edit = array();
  304. $edit['title'] = $node_tag1_title = $this->randomName();
  305. $edit[$tag_field] = 'tag1';
  306. $this->drupalPost($node_add_path, $edit, t('Save'));
  307. $edit = array();
  308. $edit['title'] = $node_tag1_tag2_title = $this->randomName();
  309. $edit[$tag_field] = 'tag1, tag2';
  310. $this->drupalPost($node_add_path, $edit, t('Save'));
  311. $edit = array();
  312. $edit['title'] = $node_no_tags_title = $this->randomName();
  313. $this->drupalPost($node_add_path, $edit, t('Save'));
  314. // Create a view that filters by taxonomy term "tag1". It should show only
  315. // the two nodes from above that are tagged with "tag1".
  316. $view1 = array();
  317. // First select the node type and update the form so the correct tag field
  318. // is used.
  319. $view1['show[type]'] = $this->node_type_with_tags->type;
  320. $this->drupalPost('admin/structure/views/add', $view1, t('Update "of type" choice'));
  321. // Now resubmit the entire form to the same URL.
  322. $view1['human_name'] = $this->randomName(16);
  323. $view1['name'] = strtolower($this->randomName(16));
  324. $view1['description'] = $this->randomName(16);
  325. $view1['show[tagged_with]'] = 'tag1';
  326. $view1['page[create]'] = 1;
  327. $view1['page[title]'] = $this->randomName(16);
  328. $view1['page[path]'] = $this->randomName(16);
  329. $this->drupalPost(NULL, $view1, t('Save & exit'));
  330. // Visit the page and check that the nodes we expect are present and the
  331. // ones we don't expect are absent.
  332. $this->drupalGet($view1['page[path]']);
  333. $this->assertText($node_tag1_title);
  334. $this->assertText($node_tag1_tag2_title);
  335. $this->assertNoText($node_no_tags_title);
  336. // Create a view that filters by taxonomy term "tag2". It should show only
  337. // the one node from above that is tagged with "tag2".
  338. $view2 = array();
  339. $view2['show[type]'] = $this->node_type_with_tags->type;
  340. $this->drupalPost('admin/structure/views/add', $view2, t('Update "of type" choice'));
  341. $view2['human_name'] = $this->randomName(16);
  342. $view2['name'] = strtolower($this->randomName(16));
  343. $view2['description'] = $this->randomName(16);
  344. $view2['show[tagged_with]'] = 'tag2';
  345. $view2['page[create]'] = 1;
  346. $view2['page[title]'] = $this->randomName(16);
  347. $view2['page[path]'] = $this->randomName(16);
  348. $this->drupalPost(NULL, $view2, t('Save & exit'));
  349. $this->drupalGet($view2['page[path]']);
  350. $this->assertNoText($node_tag1_title);
  351. $this->assertText($node_tag1_tag2_title);
  352. $this->assertNoText($node_no_tags_title);
  353. }
  354. /**
  355. * Tests that the "tagged with" form element only shows for node types that support it.
  356. */
  357. function testTaggedWithByNodeType() {
  358. // The tagging field is associated with one of our node types only. So the
  359. // "tagged with" form element on the view wizard should appear on the form
  360. // by default (when the wizard is configured to display all content) and
  361. // also when the node type that has the tagging field is selected, but not
  362. // when the node type that doesn't have the tagging field is selected.
  363. $tags_xpath = '//input[@name="show[tagged_with]"]';
  364. $this->drupalGet('admin/structure/views/add');
  365. $this->assertFieldByXpath($tags_xpath);
  366. $view['show[type]'] = $this->node_type_with_tags->type;
  367. $this->drupalPost('admin/structure/views/add', $view, t('Update "of type" choice'));
  368. $this->assertFieldByXpath($tags_xpath);
  369. $view['show[type]'] = $this->node_type_without_tags->type;
  370. $this->drupalPost(NULL, $view, t('Update "of type" choice'));
  371. $this->assertNoFieldByXpath($tags_xpath);
  372. // If we add an instance of the tagging field to the second node type, the
  373. // "tagged with" form element should not appear for it too.
  374. $instance = $this->tag_instance;
  375. $instance['bundle'] = $this->node_type_without_tags->type;
  376. field_create_instance($instance);
  377. $view['show[type]'] = $this->node_type_with_tags->type;
  378. $this->drupalPost('admin/structure/views/add', $view, t('Update "of type" choice'));
  379. $this->assertFieldByXpath($tags_xpath);
  380. $view['show[type]'] = $this->node_type_without_tags->type;
  381. $this->drupalPost(NULL, $view, t('Update "of type" choice'));
  382. $this->assertFieldByXpath($tags_xpath);
  383. }
  384. }
  385. /**
  386. * Tests the ability of the views wizard to create views with sorts.
  387. */
  388. class ViewsUIWizardSortingTestCase extends ViewsUIWizardHelper {
  389. public static function getInfo() {
  390. return array(
  391. 'name' => 'Views UI wizard sorting functionality',
  392. 'description' => 'Test the ability of the views wizard to create views with sorts.',
  393. 'group' => 'Views UI',
  394. );
  395. }
  396. /**
  397. * Tests the sorting functionality.
  398. */
  399. function testSorting() {
  400. // Create nodes, each with a different creation time so that we can do a
  401. // meaningful sort.
  402. $node1 = $this->drupalCreateNode(array('created' => REQUEST_TIME));
  403. $node2 = $this->drupalCreateNode(array('created' => REQUEST_TIME + 1));
  404. $node3 = $this->drupalCreateNode(array('created' => REQUEST_TIME + 2));
  405. // Create a view that sorts oldest first.
  406. $view1 = array();
  407. $view1['human_name'] = $this->randomName(16);
  408. $view1['name'] = strtolower($this->randomName(16));
  409. $view1['description'] = $this->randomName(16);
  410. $view1['show[sort]'] = 'created:ASC';
  411. $view1['page[create]'] = 1;
  412. $view1['page[title]'] = $this->randomName(16);
  413. $view1['page[path]'] = $this->randomName(16);
  414. $this->drupalPost('admin/structure/views/add', $view1, t('Save & exit'));
  415. // Make sure the view shows the nodes in the expected order.
  416. $this->assertUrl($view1['page[path]']);
  417. $this->assertText($view1['page[title]']);
  418. $content = $this->drupalGetContent();
  419. $this->assertText($node1->title);
  420. $this->assertText($node2->title);
  421. $this->assertText($node3->title);
  422. $pos1 = strpos($content, $node1->title);
  423. $pos2 = strpos($content, $node2->title);
  424. $pos3 = strpos($content, $node3->title);
  425. $this->assertTrue($pos1 < $pos2 && $pos2 < $pos3, t('The nodes appear in the expected order in a view that sorts by oldest first.'));
  426. // Create a view that sorts newest first.
  427. $view2 = array();
  428. $view2['human_name'] = $this->randomName(16);
  429. $view2['name'] = strtolower($this->randomName(16));
  430. $view2['description'] = $this->randomName(16);
  431. $view2['show[sort]'] = 'created:DESC';
  432. $view2['page[create]'] = 1;
  433. $view2['page[title]'] = $this->randomName(16);
  434. $view2['page[path]'] = $this->randomName(16);
  435. $this->drupalPost('admin/structure/views/add', $view2, t('Save & exit'));
  436. // Make sure the view shows the nodes in the expected order.
  437. $this->assertUrl($view2['page[path]']);
  438. $this->assertText($view2['page[title]']);
  439. $content = $this->drupalGetContent();
  440. $this->assertText($node3->title);
  441. $this->assertText($node2->title);
  442. $this->assertText($node1->title);
  443. $pos3 = strpos($content, $node3->title);
  444. $pos2 = strpos($content, $node2->title);
  445. $pos1 = strpos($content, $node1->title);
  446. $this->assertTrue($pos3 < $pos2 && $pos2 < $pos1, t('The nodes appear in the expected order in a view that sorts by newest first.'));
  447. }
  448. }
  449. /**
  450. * Tests the ability of the views wizard to specify the number of items per page.
  451. */
  452. class ViewsUIWizardItemsPerPageTestCase extends ViewsUIWizardHelper {
  453. public static function getInfo() {
  454. return array(
  455. 'name' => 'Views UI wizard items per page functionality',
  456. 'description' => 'Test the ability of the views wizard to specify the number of items per page.',
  457. 'group' => 'Views UI',
  458. );
  459. }
  460. /**
  461. * Tests the number of items per page.
  462. */
  463. function testItemsPerPage() {
  464. // Create articles, each with a different creation time so that we can do a
  465. // meaningful sort.
  466. $node1 = $this->drupalCreateNode(array('type' => 'article', 'created' => REQUEST_TIME));
  467. $node2 = $this->drupalCreateNode(array('type' => 'article', 'created' => REQUEST_TIME + 1));
  468. $node3 = $this->drupalCreateNode(array('type' => 'article', 'created' => REQUEST_TIME + 2));
  469. $node4 = $this->drupalCreateNode(array('type' => 'article', 'created' => REQUEST_TIME + 3));
  470. $node5 = $this->drupalCreateNode(array('type' => 'article', 'created' => REQUEST_TIME + 4));
  471. // Create a page. This should never appear in the view created below.
  472. $page_node = $this->drupalCreateNode(array('type' => 'page', 'created' => REQUEST_TIME + 2));
  473. // Create a view that sorts newest first, and shows 4 items in the page and
  474. // 3 in the block.
  475. $view = array();
  476. $view['human_name'] = $this->randomName(16);
  477. $view['name'] = strtolower($this->randomName(16));
  478. $view['description'] = $this->randomName(16);
  479. $view['show[wizard_key]'] = 'node';
  480. $view['show[type]'] = 'article';
  481. $view['show[sort]'] = 'created:DESC';
  482. $view['page[create]'] = 1;
  483. $view['page[title]'] = $this->randomName(16);
  484. $view['page[path]'] = $this->randomName(16);
  485. $view['page[items_per_page]'] = 4;
  486. $view['block[create]'] = 1;
  487. $view['block[title]'] = $this->randomName(16);
  488. $view['block[items_per_page]'] = 3;
  489. $this->drupalPost('admin/structure/views/add', $view, t('Save & exit'));
  490. // Make sure the page display shows the nodes we expect, and that they
  491. // appear in the expected order.
  492. $this->assertUrl($view['page[path]']);
  493. $this->assertText($view['page[title]']);
  494. $content = $this->drupalGetContent();
  495. $this->assertText($node5->title);
  496. $this->assertText($node4->title);
  497. $this->assertText($node3->title);
  498. $this->assertText($node2->title);
  499. $this->assertNoText($node1->title);
  500. $this->assertNoText($page_node->title);
  501. $pos5 = strpos($content, $node5->title);
  502. $pos4 = strpos($content, $node4->title);
  503. $pos3 = strpos($content, $node3->title);
  504. $pos2 = strpos($content, $node2->title);
  505. $this->assertTrue($pos5 < $pos4 && $pos4 < $pos3 && $pos3 < $pos2, t('The nodes appear in the expected order in the page display.'));
  506. // Put the block into the first sidebar region, visit a page that displays
  507. // the block, and check that the nodes we expect appear in the correct
  508. // order.
  509. $this->drupalGet('admin/structure/block');
  510. $this->assertText('View: ' . $view['human_name']);
  511. $edit = array();
  512. $edit["blocks[views_{$view['name']}-block][region]"] = 'sidebar_first';
  513. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  514. $this->drupalGet('user');
  515. $content = $this->drupalGetContent();
  516. $this->assertText($node5->title);
  517. $this->assertText($node4->title);
  518. $this->assertText($node3->title);
  519. $this->assertNoText($node2->title);
  520. $this->assertNoText($node1->title);
  521. $this->assertNoText($page_node->title);
  522. $pos5 = strpos($content, $node5->title);
  523. $pos4 = strpos($content, $node4->title);
  524. $pos3 = strpos($content, $node3->title);
  525. $this->assertTrue($pos5 < $pos4 && $pos4 < $pos3, t('The nodes appear in the expected order in the block display.'));
  526. }
  527. }
  528. /**
  529. * Tests the ability of the views wizard to put views in a menu.
  530. */
  531. class ViewsUIWizardMenuTestCase extends ViewsUIWizardHelper {
  532. public static function getInfo() {
  533. return array(
  534. 'name' => 'Views UI wizard menu functionality',
  535. 'description' => 'Test the ability of the views wizard to put views in a menu.',
  536. 'group' => 'Views UI',
  537. );
  538. }
  539. /**
  540. * Tests the menu functionality.
  541. */
  542. function testMenus() {
  543. // Create a view with a page display and a menu link in the Main Menu.
  544. $view = array();
  545. $view['human_name'] = $this->randomName(16);
  546. $view['name'] = strtolower($this->randomName(16));
  547. $view['description'] = $this->randomName(16);
  548. $view['page[create]'] = 1;
  549. $view['page[title]'] = $this->randomName(16);
  550. $view['page[path]'] = $this->randomName(16);
  551. $view['page[link]'] = 1;
  552. $view['page[link_properties][menu_name]'] = 'main-menu';
  553. $view['page[link_properties][title]'] = $this->randomName(16);
  554. $this->drupalPost('admin/structure/views/add', $view, t('Save & exit'));
  555. // Make sure there is a link to the view from the front page (where we
  556. // expect the main menu to display).
  557. $this->drupalGet('');
  558. $this->assertLink($view['page[link_properties][title]']);
  559. $this->assertLinkByHref(url($view['page[path]']));
  560. // Make sure the link is associated with the main menu.
  561. $links = menu_load_links('main-menu');
  562. $found = FALSE;
  563. foreach ($links as $link) {
  564. if ($link['link_path'] == $view['page[path]']) {
  565. $found = TRUE;
  566. break;
  567. }
  568. }
  569. $this->assertTrue($found, t('Found a link to %path in the main menu', array('%path' => $view['page[path]'])));
  570. }
  571. }
  572. /**
  573. * Tests the ability of the views wizard to create views with a jump menu style plugin.
  574. */
  575. class ViewsUIWizardJumpMenuTestCase extends ViewsUIWizardHelper {
  576. public static function getInfo() {
  577. return array(
  578. 'name' => 'Views UI wizard jump menu functionality',
  579. 'description' => 'Test the ability of the views wizard to create views with a jump menu style plugin.',
  580. 'group' => 'Views UI',
  581. );
  582. }
  583. /**
  584. * Tests the jump menu style plugin.
  585. */
  586. function testJumpMenus() {
  587. // We'll run this test for several different base tables that appear in the
  588. // wizard.
  589. $base_table_methods = array(
  590. 'node' => 'createNodeAndGetPath',
  591. 'users' => 'createUserAndGetPath',
  592. 'comment' => 'createCommentAndGetPath',
  593. 'taxonomy_term' => 'createTaxonomyTermAndGetPath',
  594. 'file_managed' => 'createFileAndGetPath',
  595. 'node_revision' => 'createNodeRevisionAndGetPath',
  596. );
  597. foreach ($base_table_methods as $base_table => $method) {
  598. // For each base table, find the path that we expect the jump menu to
  599. // redirect us to.
  600. $path_info = $this->{$method}();
  601. if (is_array($path_info)) {
  602. $path = $path_info['path'];
  603. $options = isset($path_info['options']) ? $path_info['options'] : array();
  604. }
  605. else {
  606. $path = $path_info;
  607. $options = array();
  608. }
  609. // Create a page view for the specified base table that uses the jump
  610. // menu style plugin.
  611. $view = array();
  612. $view['human_name'] = $this->randomName(16);
  613. $view['name'] = strtolower($this->randomName(16));
  614. $view['description'] = $this->randomName(16);
  615. $view['show[wizard_key]'] = $base_table;
  616. $view['page[create]'] = 1;
  617. $view['page[title]'] = $this->randomName(16);
  618. $view['page[path]'] = $this->randomName(16);
  619. $view['page[style][style_plugin]'] = 'jump_menu';
  620. $view['page[style][row_plugin]'] = 'fields';
  621. $this->drupalPost('admin/structure/views/add', $view, t('Save & exit'));
  622. // Submit the jump menu form, and check that we are redirected to the
  623. // expected URL.
  624. $edit = array();
  625. $edit['jump'] = url($path, $options);
  626. // The urls are built with :: to be able to have a unique path all the time,
  627. // so try to find out the real path of $edit.
  628. $view_object = views_get_view($view['name']);
  629. $view_object->preview('page');
  630. $form = $view_object->style_plugin->render();
  631. $jump_options = $form['jump']['#options'];
  632. foreach ($jump_options as $key => $title) {
  633. if (strpos($key, $edit['jump']) !== FALSE) {
  634. $edit['jump'] = $key;
  635. }
  636. }
  637. $this->drupalPost($view['page[path]'], $edit, t('Go'));
  638. $this->assertResponse(200);
  639. $this->assertUrl($path, $options);
  640. }
  641. }
  642. /**
  643. * Helper function to create a node and return its expected path.
  644. */
  645. function createNodeAndGetPath() {
  646. $node = $this->drupalCreateNode();
  647. return entity_uri('node', $node);
  648. }
  649. /**
  650. * Helper function to create a user and return its expected path.
  651. */
  652. function createUserAndGetPath() {
  653. $account = $this->drupalCreateUser();
  654. return entity_uri('user', $account);
  655. }
  656. /**
  657. * Helper function to create a comment and return its expected path.
  658. */
  659. function createCommentAndGetPath() {
  660. $node = $this->drupalCreateNode();
  661. $comment = (object) array(
  662. 'cid' => NULL,
  663. 'nid' => $node->nid,
  664. 'pid' => 0,
  665. 'uid' => 0,
  666. 'status' => COMMENT_PUBLISHED,
  667. 'subject' => $this->randomName(),
  668. 'language' => LANGUAGE_NONE,
  669. 'comment_body' => array(LANGUAGE_NONE => array($this->randomName())),
  670. );
  671. comment_save($comment);
  672. return entity_uri('comment', $comment);
  673. }
  674. /**
  675. * Helper function to create a taxonomy term and return its expected path.
  676. */
  677. function createTaxonomyTermAndGetPath() {
  678. $vocabulary = new stdClass();
  679. $vocabulary->name = $this->randomName();
  680. $vocabulary->machine_name = drupal_strtolower($this->randomName());
  681. taxonomy_vocabulary_save($vocabulary);
  682. $term = new stdClass();
  683. $term->name = $this->randomName();
  684. $term->vid = $vocabulary->vid;
  685. taxonomy_term_save($term);
  686. return entity_uri('taxonomy_term', $term);
  687. }
  688. /**
  689. * Helper function to create a file and return its expected path.
  690. */
  691. function createFileAndGetPath() {
  692. $file = (object) array(
  693. 'uid' => 1,
  694. 'filename' => 'views-ui-jump-menu-test.txt',
  695. 'uri' => 'public://views-ui-jump-menu-test.txt',
  696. 'filemime' => 'text/plain',
  697. 'timestamp' => 1,
  698. 'status' => FILE_STATUS_PERMANENT,
  699. );
  700. file_put_contents($file->uri, 'test content');
  701. $file = file_save($file);
  702. return file_create_url($file->uri);
  703. }
  704. /**
  705. * Helper function to create a node revision and return its expected path.
  706. */
  707. function createNodeRevisionAndGetPath() {
  708. // The node needs at least two revisions in order for Drupal to allow
  709. // access to the revision path.
  710. $settings = array('revision' => TRUE);
  711. $node = $this->drupalCreateNode($settings);
  712. $node->vid = NULL;
  713. node_save($node);
  714. return 'node/' . $node->nid . '/revisions/' . $node->vid . '/view';
  715. }
  716. }
  717. /**
  718. * Tests that displays can be correctly overridden via the user interface.
  719. */
  720. class ViewsUIWizardOverrideDisplaysTestCase extends ViewsUIWizardHelper {
  721. public static function getInfo() {
  722. return array(
  723. 'name' => 'Views UI overridden displays',
  724. 'description' => 'Test that displays can be correctly overridden via the user interface.',
  725. 'group' => 'Views UI',
  726. );
  727. }
  728. /**
  729. * Tests that displays can be overridden via the UI.
  730. */
  731. function testOverrideDisplays() {
  732. // Create a basic view that shows all content, with a page and a block
  733. // display.
  734. $view['human_name'] = $this->randomName(16);
  735. $view['name'] = strtolower($this->randomName(16));
  736. $view['page[create]'] = 1;
  737. $view['page[path]'] = $this->randomName(16);
  738. $view['block[create]'] = 1;
  739. $view_path = $view['page[path]'];
  740. $this->drupalPost('admin/structure/views/add', $view, t('Save & exit'));
  741. // Configure its title. Since the page and block both started off with the
  742. // same (empty) title in the views wizard, we expect the wizard to have set
  743. // things up so that they both inherit from the default display, and we
  744. // therefore only need to change that to have it take effect for both.
  745. $edit = array();
  746. $edit['title'] = $original_title = $this->randomName(16);
  747. $edit['override[dropdown]'] = 'default';
  748. $this->drupalPost("admin/structure/views/nojs/display/{$view['name']}/page/title", $edit, t('Apply'));
  749. $this->drupalPost("admin/structure/views/view/{$view['name']}/edit/page", array(), t('Save'));
  750. // Put the block into the first sidebar region, and make sure it will not
  751. // display on the view's page display (since we will be searching for the
  752. // presence/absence of the view's title in both the page and the block).
  753. $this->drupalGet('admin/structure/block');
  754. $edit = array();
  755. $edit["blocks[views_{$view['name']}-block][region]"] = 'sidebar_first';
  756. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  757. $edit = array();
  758. $edit['visibility'] = BLOCK_VISIBILITY_NOTLISTED;
  759. $edit['pages'] = $view_path;
  760. $this->drupalPost("admin/structure/block/manage/views/{$view['name']}-block/configure", $edit, t('Save block'));
  761. // Add a node that will appear in the view, so that the block will actually
  762. // be displayed.
  763. $this->drupalCreateNode();
  764. // Make sure the title appears in both the page and the block.
  765. $this->drupalGet($view_path);
  766. $this->assertText($original_title);
  767. $this->drupalGet('');
  768. $this->assertText($original_title);
  769. // Change the title for the page display only, and make sure that is the
  770. // only one that is changed.
  771. $edit = array();
  772. $edit['title'] = $new_title = $this->randomName(16);
  773. $edit['override[dropdown]'] = 'page';
  774. $this->drupalPost("admin/structure/views/nojs/display/{$view['name']}/page/title", $edit, t('Apply'));
  775. $this->drupalPost("admin/structure/views/view/{$view['name']}/edit/page", array(), t('Save'));
  776. $this->drupalGet($view_path);
  777. $this->assertText($new_title);
  778. $this->assertNoText($original_title);
  779. $this->drupalGet('');
  780. $this->assertText($original_title);
  781. $this->assertNoText($new_title);
  782. }
  783. /**
  784. * Tests that the wizard correctly sets up default and overridden displays.
  785. */
  786. function testWizardMixedDefaultOverriddenDisplays() {
  787. // Create a basic view with a page, block, and feed. Give the page and feed
  788. // identical titles, but give the block a different one, so we expect the
  789. // page and feed to inherit their titles from the default display, but the
  790. // block to override it.
  791. $view['human_name'] = $this->randomName(16);
  792. $view['name'] = strtolower($this->randomName(16));
  793. $view['page[create]'] = 1;
  794. $view['page[title]'] = $this->randomName(16);
  795. $view['page[path]'] = $this->randomName(16);
  796. $view['page[feed]'] = 1;
  797. $view['page[feed_properties][path]'] = $this->randomName(16);
  798. $view['block[create]'] = 1;
  799. $view['block[title]'] = $this->randomName(16);
  800. $this->drupalPost('admin/structure/views/add', $view, t('Save & exit'));
  801. // Put the block into the first sidebar region, and make sure it will not
  802. // display on the view's page display (since we will be searching for the
  803. // presence/absence of the view's title in both the page and the block).
  804. $this->drupalGet('admin/structure/block');
  805. $edit = array();
  806. $edit["blocks[views_{$view['name']}-block][region]"] = 'sidebar_first';
  807. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  808. $edit = array();
  809. $edit['visibility'] = BLOCK_VISIBILITY_NOTLISTED;
  810. $edit['pages'] = $view['page[path]'];
  811. $this->drupalPost("admin/structure/block/manage/views/{$view['name']}-block/configure", $edit, t('Save block'));
  812. // Add a node that will appear in the view, so that the block will actually
  813. // be displayed.
  814. $this->drupalCreateNode();
  815. // Make sure that the feed, page and block all start off with the correct
  816. // titles.
  817. $this->drupalGet($view['page[path]']);
  818. $this->assertText($view['page[title]']);
  819. $this->assertNoText($view['block[title]']);
  820. $this->drupalGet($view['page[feed_properties][path]']);
  821. $this->assertText($view['page[title]']);
  822. $this->assertNoText($view['block[title]']);
  823. $this->drupalGet('');
  824. $this->assertText($view['block[title]']);
  825. $this->assertNoText($view['page[title]']);
  826. // Edit the page and change the title. This should automatically change
  827. // the feed's title also, but not the block.
  828. $edit = array();
  829. $edit['title'] = $new_default_title = $this->randomName(16);
  830. $this->drupalPost("admin/structure/views/nojs/display/{$view['name']}/page/title", $edit, t('Apply'));
  831. $this->drupalPost("admin/structure/views/view/{$view['name']}/edit/page", array(), t('Save'));
  832. $this->drupalGet($view['page[path]']);
  833. $this->assertText($new_default_title);
  834. $this->assertNoText($view['page[title]']);
  835. $this->assertNoText($view['block[title]']);
  836. $this->drupalGet($view['page[feed_properties][path]']);
  837. $this->assertText($new_default_title);
  838. $this->assertNoText($view['page[title]']);
  839. $this->assertNoText($view['block[title]']);
  840. $this->drupalGet('');
  841. $this->assertText($view['block[title]']);
  842. $this->assertNoText($new_default_title);
  843. $this->assertNoText($view['page[title]']);
  844. // Edit the block and change the title. This should automatically change
  845. // the block title only, and leave the defaults alone.
  846. $edit = array();
  847. $edit['title'] = $new_block_title = $this->randomName(16);
  848. $this->drupalPost("admin/structure/views/nojs/display/{$view['name']}/block/title", $edit, t('Apply'));
  849. $this->drupalPost("admin/structure/views/view/{$view['name']}/edit/block", array(), t('Save'));
  850. $this->drupalGet($view['page[path]']);
  851. $this->assertText($new_default_title);
  852. $this->assertNoText($new_block_title);
  853. $this->drupalGet($view['page[feed_properties][path]']);
  854. $this->assertText($new_default_title);
  855. $this->assertNoText($new_block_title);
  856. $this->drupalGet('');
  857. $this->assertText($new_block_title);
  858. $this->assertNoText($view['block[title]']);
  859. }
  860. /**
  861. * Tests that the revert to all displays select-option works as expected.
  862. */
  863. function testRevertAllDisplays() {
  864. // Create a basic view with a page, block.
  865. // Because there is both a title on page and block we expect the title on
  866. // the block be overriden.
  867. $view['human_name'] = $this->randomName(16);
  868. $view['name'] = strtolower($this->randomName(16));
  869. $view['page[create]'] = 1;
  870. $view['page[title]'] = $this->randomName(16);
  871. $view['page[path]'] = $this->randomName(16);
  872. $view['block[create]'] = 1;
  873. $view['block[title]'] = $this->randomName(16);
  874. $this->drupalPost('admin/structure/views/add', $view, t('Continue & edit'));
  875. // Revert the title of the block back to the default ones, but submit some
  876. // new values to be sure that the new value is not stored.
  877. $edit = array();
  878. $edit['title'] = $new_block_title = $this->randomName();
  879. $edit['override[dropdown]'] = 'default_revert';
  880. $this->drupalPost("admin/structure/views/nojs/display/{$view['name']}/block/title", $edit, t('Apply'));
  881. $this->drupalPost("admin/structure/views/view/{$view['name']}/edit/block", array(), t('Save'));
  882. $this->assertText($view['page[title]']);
  883. }
  884. }