views_ui.test 39 KB

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