uc_cart_links.test 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. <?php
  2. /**
  3. * @file
  4. * Ubercart Cart Links Tests.
  5. */
  6. /**
  7. * SimpleTests for Ubercart Cart Links.
  8. */
  9. class UbercartCartLinksTestCase extends UbercartTestHelper {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Cart Links',
  13. 'description' => 'Test Cart Links.',
  14. 'group' => 'Ubercart',
  15. );
  16. }
  17. /**
  18. * Overrides DrupalWebTestCase::setUp().
  19. */
  20. protected function setUp($modules = array(), $permissions = array()) {
  21. parent::setUp(array('uc_cart_links', 'uc_attribute'), array('administer cart links', 'view cart links report', 'access administration pages'));
  22. }
  23. /**
  24. * Tests access to admin settings page and tests default values.
  25. */
  26. function testCartLinksUISettingsPage() {
  27. // Access settings page by anonymous user
  28. $this->drupalGet('admin/store/settings/cart-links');
  29. $this->assertText(t('Access denied'));
  30. $this->assertText(t('You are not authorized to access this page.'));
  31. // Access settings page by privileged user
  32. $this->drupalLogin($this->adminUser);
  33. $this->drupalGet('admin/store/settings/cart-links');
  34. $this->assertText(
  35. t('View the help page to learn how to create Cart Links.'),
  36. t('Settings page found.')
  37. );
  38. $this->assertFieldByName(
  39. 'uc_cart_links_add_show',
  40. 0,
  41. t('Display Cart Links product action messages is off.')
  42. );
  43. $this->assertFieldByName(
  44. 'uc_cart_links_track',
  45. 1,
  46. t('Track clicks is on.')
  47. );
  48. $this->assertFieldByName(
  49. 'uc_cart_links_empty',
  50. 1,
  51. t('Allow Cart Links to empty carts is on.')
  52. );
  53. $this->assertFieldByName(
  54. 'uc_cart_links_messages',
  55. '',
  56. t('Cart Links messages is empty.')
  57. );
  58. $this->assertFieldByName(
  59. 'uc_cart_links_restrictions',
  60. '',
  61. t('Cart Links restrictions is empty.')
  62. );
  63. // Test presence of and contents of Help page
  64. $this->clickLink(t('View the help page'));
  65. $this->assertText(
  66. 'http://www.example.com/cart/add/&lt;cart_link_content&gt;',
  67. t('Help text found.')
  68. );
  69. }
  70. /**
  71. * Tests Cart Links on a page under a variety of conditions.
  72. */
  73. function testCartLinksBasicFunctionality() {
  74. // Create product
  75. $products[] = $this->createCartLinksProduct(FALSE);
  76. // Create a product class
  77. $products[] = $this->createCartLinksProduct(FALSE); // later ...
  78. // Create some valid Cart Links for these products
  79. $link_array = $this->createValidCartLinks($products);
  80. $cart_links = $link_array['links'];
  81. $link_data = $link_array['data'];
  82. // Need to test incorrect links as well:
  83. // links which add invalid attributes
  84. // links which omit required attributes
  85. // Create a page containing these links
  86. $page = $this->createCartLinksPage($cart_links);
  87. //
  88. // Test clicking on links
  89. //
  90. foreach ($cart_links as $key => $test_link) {
  91. $this->drupalGet('node/' . $page->nid);
  92. // Look for link on page
  93. $this->assertLink(
  94. t('Cart Link #@link', array('@link' => $key)),
  95. 0,
  96. t('Cart Link #@link found on page.', array('@link' => $key))
  97. );
  98. // Note we strip the leading / from the link for the testbot ...
  99. $this->assertLinkByHref(
  100. t('@link', array('@link' => substr($test_link, 1))),
  101. 0,
  102. t('Cart Link @link found on page.', array('@link' => $test_link))
  103. );
  104. // Click on link
  105. $this->clickLink(t('Cart Link #@link', array('@link' => $key)));
  106. // Check for notice that item was added (this notice is set ON
  107. // by default, see admin/store/settings/cart)
  108. $this->assertText(
  109. t('@title added to your shopping cart.', array('@title' => $link_data[$key]['title'])),
  110. t('Product @title added to cart.', array('@title' => $link_data[$key]['title']))
  111. );
  112. // Check contents of cart
  113. $this->drupalGet('cart');
  114. $this->assertText(
  115. $link_data[$key]['title'],
  116. t('Product title correct in cart.')
  117. );
  118. $this->assertFieldByName(
  119. 'items[0][qty]',
  120. $link_data[$key]['qty'],
  121. t('Product quantity correct in cart.')
  122. );
  123. // Check for correct attribute name(s) in cart
  124. foreach ($link_data[$key]['attributes'] as $label => $attribute) {
  125. $this->assertText(
  126. $label . ':',
  127. t('Attribute @label correct in cart.', array('@label' => $label))
  128. );
  129. foreach ($attribute as $option) {
  130. // Check for correct option name(s) in cart
  131. $this->assertText(
  132. $option,
  133. t('Option @name correct in cart.', array('@name' => $option))
  134. );
  135. }
  136. }
  137. // Use the same link, but this time append an '_s' to turn
  138. // off message display for this product
  139. $this->drupalGet($test_link . '_s');
  140. // Default add-to-cart message is different when adding a duplicate item
  141. $this->assertNoText(
  142. t('Your item(s) have been updated.'),
  143. t('Default add-to-cart message suppressed.')
  144. );
  145. // Empty cart (press remove button)
  146. $this->drupalPost('cart', array(), t('Remove'));
  147. $this->assertText('There are no products in your shopping cart.');
  148. }
  149. }
  150. /**
  151. * Tests Cart Links product action messages.
  152. */
  153. function testCartLinksProductActionMessage() {
  154. // Create product
  155. $products[] = $this->createCartLinksProduct(FALSE);
  156. // Create a product class
  157. $products[] = $this->createCartLinksProduct(FALSE); // later ...
  158. // Create some valid Cart Links for these products
  159. $link_array = $this->createValidCartLinks($products);
  160. $cart_links = $link_array['links'];
  161. $link_data = $link_array['data'];
  162. // Create a page containing these links
  163. $page = $this->createCartLinksPage($cart_links);
  164. $this->drupalLogin($this->adminUser);
  165. //
  166. // Test product action message display
  167. //
  168. // Turn on display of product action message
  169. $this->setCartLinksUIProductActionMessage(TRUE);
  170. // Go to page with Cart Links
  171. $this->drupalGet('node/' . $page->nid);
  172. // Pick one of the links at random
  173. $test_link = array_rand($cart_links);
  174. $this->clickLink(t('Cart Link #@link', array('@link' => $test_link)));
  175. $this->assertText(
  176. t('Cart Link product action: @link', array('@link' => substr($cart_links[$test_link], 10))),
  177. t('Cart Link product action message found.')
  178. );
  179. // Empty cart (press remove button)
  180. $this->drupalPost('cart', array(), t('Remove'));
  181. $this->assertText('There are no products in your shopping cart.');
  182. // Turn off display of product action message
  183. $this->setCartLinksUIProductActionMessage(FALSE);
  184. // Go to page with Cart Links
  185. $this->drupalGet('node/' . $page->nid);
  186. // Pick one of the links at random
  187. $test_link = array_rand($cart_links);
  188. $this->clickLink(t('Cart Link #@link', array('@link' => $test_link)));
  189. $this->assertNoText(
  190. t('Cart Link product action: @link', array('@link' => substr($cart_links[$test_link], 10))),
  191. t('Cart Link product action message not present.')
  192. );
  193. $this->drupalLogout();
  194. }
  195. /**
  196. * Tests Cart Links cart empty action.
  197. */
  198. function testCartLinksAllowEmptying() {
  199. // Create product
  200. $products[] = $this->createCartLinksProduct(FALSE);
  201. // Create a product class
  202. $products[] = $this->createCartLinksProduct(FALSE); // later ...
  203. // Create some valid Cart Links for these products
  204. $link_array = $this->createValidCartLinks($products);
  205. $cart_links = $link_array['links'];
  206. $link_data = $link_array['data'];
  207. // Create a page containing these links
  208. $page = $this->createCartLinksPage($cart_links);
  209. $this->drupalLogin($this->adminUser);
  210. //
  211. // Test empty cart action
  212. //
  213. // Allow links to empty cart
  214. $this->setCartLinksUIAllowEmptying(TRUE);
  215. // Go to page with Cart Links
  216. $this->drupalGet('node/' . $page->nid);
  217. // Pick one of the links at random and add it to the cart
  218. $test_link_0 = array_rand($cart_links);
  219. $this->clickLink(t('Cart Link #@link', array('@link' => $test_link_0)));
  220. // Pick another link at random and prepend an 'e-' so it will empty cart
  221. $in_cart = $cart_links[$test_link_0];
  222. // (Don't want to use the same link.)
  223. unset($cart_links[$test_link_0]);
  224. $test_link = array_rand($cart_links);
  225. $this->drupalGet(str_replace('add/p', 'add/e-p', $cart_links[$test_link]));
  226. $this->assertText(
  227. t('The current contents of your shopping cart will be lost. Are you sure you want to continue?'),
  228. t('Empty cart confirmation page found.')
  229. );
  230. // Allow
  231. $this->drupalPost(NULL, array(), t('Confirm'));
  232. // Verify the cart doesn't have the first item and does have the second item
  233. $this->drupalGet('cart');
  234. $this->assertText(
  235. $link_data[$test_link]['title'],
  236. t('Product title correct in cart.')
  237. );
  238. $this->assertNoText(
  239. $link_data[$test_link_0]['title'],
  240. t('Cart was emptied by Cart Link.')
  241. );
  242. // Still have something ($test_link) in the cart
  243. // Forbid links to empty cart
  244. $this->setCartLinksUIAllowEmptying(FALSE);
  245. // Re-use $test_link_0 and prepend an 'e-' so it will (try to) empty cart
  246. $this->drupalGet(str_replace('add/p', 'add/e-p', $in_cart));
  247. // Verify the cart has both items - cart wasn't emptied
  248. $this->drupalGet('cart');
  249. $this->assertText(
  250. $link_data[$test_link_0]['title'],
  251. t('Cart was not emptied by Cart Link.')
  252. );
  253. $this->assertText(
  254. $link_data[$test_link]['title'],
  255. t('Cart was not emptied by Cart Link.')
  256. );
  257. $this->drupalLogout();
  258. }
  259. /**
  260. * Tests Cart Links restrictions.
  261. */
  262. function testCartLinksRestrictions() {
  263. // Create product
  264. $products[] = $this->createCartLinksProduct(FALSE);
  265. // Create a product class
  266. $products[] = $this->createCartLinksProduct(FALSE); // later ...
  267. // Create some valid Cart Links for these products
  268. $link_array = $this->createValidCartLinks($products);
  269. $cart_links = $link_array['links'];
  270. $link_data = $link_array['data'];
  271. // Create a page containing these links
  272. $page = $this->createCartLinksPage($cart_links);
  273. $this->drupalLogin($this->adminUser);
  274. //
  275. // Test Cart Links restrictions
  276. //
  277. // Go to page with Cart Links
  278. $this->drupalGet('node/' . $page->nid);
  279. // Pick one of the links at random and restrict it
  280. $test_link_0 = array_rand($cart_links);
  281. // Only this link is allowed - strip '/cart/add/' from beginning
  282. $this->setCartLinksUIRestrictions(substr($cart_links[$test_link_0], 10));
  283. // Attempt to click link - should pass
  284. $this->drupalGet('node/' . $page->nid);
  285. $this->clickLink(t('Cart Link #@link', array('@link' => $test_link_0)));
  286. // Check for notice that item was added (this notice is set ON
  287. // by default, see admin/store/settings/cart)
  288. $this->assertText(
  289. t('@title added to your shopping cart.', array('@title' => $link_data[$test_link_0]['title'])),
  290. t('Product @title added to cart.', array('@title' => $link_data[$test_link_0]['title']))
  291. );
  292. // Pick another link at random, as long as it is different from first
  293. $in_cart = $cart_links[$test_link_0];
  294. unset($cart_links[$test_link_0]);
  295. $test_link = array_rand($cart_links);
  296. // Attempt to click it
  297. // It should fail and redirect to the home page (default)
  298. $this->drupalGet('node/' . $page->nid);
  299. $this->clickLink(t('Cart Link #@link', array('@link' => $test_link)));
  300. $this->assertText(
  301. t('Welcome to Drupal')
  302. );
  303. $this->assertText(
  304. t('No front page content has been created yet.'),
  305. t('Redirected to front page for link not in restrictions.')
  306. );
  307. // Now create a special redirect page for bad links
  308. $redirect_page = $this->drupalCreateNode(
  309. array(
  310. 'body' => array(
  311. LANGUAGE_NONE => array(
  312. array('value' => 'ERROR: Invalid Cart Link!')
  313. )
  314. )
  315. )
  316. );
  317. // Set redirect link
  318. $this->setCartLinksUIRedirect('node/' . $redirect_page->nid);
  319. // Attempt to click same restricted link as above.
  320. // It should fail again but this time redirect to $redirect_page.
  321. $this->drupalGet('node/' . $page->nid);
  322. $this->clickLink(t('Cart Link #@link', array('@link' => $test_link)));
  323. $this->assertText(
  324. t('ERROR: Invalid Cart Link!'),
  325. t('Redirected to error page for link not in restrictions.')
  326. );
  327. // Remove restrictions, try to add again - it should pass
  328. $this->setCartLinksUIRestrictions('');
  329. $this->drupalGet('node/' . $page->nid);
  330. $this->clickLink(t('Cart Link #@link', array('@link' => $test_link)));
  331. $this->assertText(
  332. t('@title added to your shopping cart.', array('@title' => $link_data[$test_link]['title'])),
  333. t('Product @title added to cart.', array('@title' => $link_data[$test_link]['title']))
  334. );
  335. $this->drupalLogout();
  336. }
  337. /**
  338. * Tests Cart Links messages.
  339. *
  340. * To stop the default "xxx was added to your shopping cart" message for
  341. * a product, use the argument "_s". For example /cart/add/p23_s
  342. * "_s" is an argument to the "p" action, and suppresses the message for this
  343. * product only. Other products added by other actions in the Cart Link
  344. * will still show the message. e.g. /cart/add/p23_s-p15 will show a message
  345. * for product 15 but not for product 23.
  346. *
  347. * To insert your own message, first define your message in the Cart Links
  348. * messages panel on the Cart Links settings page, by entering for example
  349. * "99|My message text". Then use the action "-m99" (a dash, not an
  350. * underscore) to add the message. For example, /cart/add/p23-m99
  351. *
  352. * Note that just specifying "-m99" will display both your message 99 and the
  353. * default message, unless you have turned off the default message with "_s".
  354. *
  355. * For additional messages, add additional actions, e.g. "-m99-m1337".
  356. */
  357. function testCartLinksMessages() {
  358. // Create product
  359. $products[] = $this->createCartLinksProduct(FALSE);
  360. // Create a product class
  361. $products[] = $this->createCartLinksProduct(FALSE); // later ...
  362. // Create some valid Cart Links for these products
  363. $link_array = $this->createValidCartLinks($products);
  364. $cart_links = $link_array['links'];
  365. $link_data = $link_array['data'];
  366. // Create a page containing these links
  367. $page = $this->createCartLinksPage($cart_links);
  368. // Need to be admin to define messages
  369. $this->drupalLogin($this->adminUser);
  370. // Define some messages
  371. $messages = array();
  372. for ($i = 0; $i < 15; $i++) {
  373. $key = mt_rand(1, 999);
  374. $messages[$key] = $key . '|' . $this->randomName(32);
  375. }
  376. $this->setCartLinksUIMessages($messages);
  377. //
  378. // Test message display
  379. //
  380. // Go to page with Cart Links
  381. $this->drupalGet('node/' . $page->nid);
  382. // Pick one link at random and append an '-m<#>' to display a message
  383. $test_link = array_rand($cart_links);
  384. $message_key = array_rand($messages);
  385. $message_text = explode('|', $messages[$message_key]);
  386. $this->drupalGet($cart_links[$test_link] . '-m' . $message_key);
  387. $this->assertText(
  388. t('@message', array('@message' => $message_text[1])),
  389. t('Message @key displayed.', array('@key' => $message_key))
  390. );
  391. // Empty cart (press remove button)
  392. $this->drupalPost('cart', array(), t('Remove'));
  393. $this->assertText('There are no products in your shopping cart.');
  394. $this->drupalLogout();
  395. }
  396. /**
  397. * Tests Cart Links tracking.
  398. */
  399. function testCartLinksTracking() {
  400. // Create product
  401. $products[] = $this->createCartLinksProduct(FALSE);
  402. // Create a product class
  403. $products[] = $this->createCartLinksProduct(FALSE); // later ...
  404. // Create some valid Cart Links for these products
  405. $link_array = $this->createValidCartLinks($products);
  406. $cart_links = $link_array['links'];
  407. $link_data = $link_array['data'];
  408. // Create a page containing these links
  409. $page = $this->createCartLinksPage($cart_links);
  410. $this->drupalLogin($this->adminUser);
  411. //
  412. // Test Cart Links tracking
  413. //
  414. // Go to page with Cart Links
  415. $this->drupalGet('node/' . $page->nid);
  416. // Create three tracking IDs
  417. $tracking = array();
  418. for ($i = 0; $i < 3; $i++) {
  419. $tracking[$this->randomName(16)] = 0;
  420. }
  421. // Click a number of links to create some statistics
  422. for ($i = 0; $i < 50; $i++) {
  423. // Pick one link at random and append an '-i<tracking ID>'
  424. $test_link = array_rand($cart_links);
  425. // Assign one of the tracking IDs
  426. $tracking_id = array_rand($tracking);
  427. $this->drupalGet($cart_links[$test_link] . '-i' . $tracking_id);
  428. // Keep a record of how many links were assigned this key
  429. $tracking[$tracking_id] += 1;
  430. }
  431. // Check report to see these clicks have been recorded correctly
  432. $this->drupalGet('admin/store/reports/cart-links');
  433. $total = 0;
  434. foreach ($tracking as $id => $clicks) {
  435. $total += $clicks;
  436. $this->assertRaw(
  437. t('<td>@id</td><td>@clicks</td>', array('@id' => $id, '@clicks' => $clicks)),
  438. t('Tracking ID @id received @clicks clicks.', array('@id' => $id, '@clicks' => $clicks))
  439. );
  440. }
  441. $this->assertEqual($total, 50, t('Fifty clicks recorded.'));
  442. $this->drupalLogout();
  443. }
  444. /****************************************************************************
  445. * Utility Functions *
  446. ****************************************************************************/
  447. /**
  448. * Sets checkbox to display Cart Links product action messages.
  449. *
  450. * Must be logged in with 'administer cart links' permission.
  451. *
  452. * @param $state
  453. * TRUE to display product action messages, FALSE to not display.
  454. * Defaults to FALSE.
  455. */
  456. function setCartLinksUIProductActionMessage($state = FALSE) {
  457. $this->drupalPost(
  458. 'admin/store/settings/cart-links',
  459. array('uc_cart_links_add_show' => $state),
  460. t('Save configuration')
  461. );
  462. $this->assertFieldByName(
  463. 'uc_cart_links_add_show',
  464. $state,
  465. t('Display Cart Links product action messages is @state.', array('@state' => $state ? 'TRUE' : 'FALSE'))
  466. );
  467. }
  468. /**
  469. * Sets checkbox to track Cart Links clicks.
  470. *
  471. * Must be logged in with 'administer cart links' permission.
  472. *
  473. * @param $state
  474. * TRUE to display product action messages, FALSE to not display.
  475. * Defaults to TRUE.
  476. */
  477. function setCartLinksUITrackClicks($state = TRUE) {
  478. $this->drupalPost(
  479. 'admin/store/settings/cart-links',
  480. array('uc_cart_links_track' => 0),
  481. t('Save configuration')
  482. );
  483. $this->assertFieldByName(
  484. 'uc_cart_links_track',
  485. $state ? 1 : 0,
  486. t('Track clicks is @state.', array('@state' => $state ? 'TRUE' : 'FALSE'))
  487. );
  488. }
  489. /**
  490. * Sets checkbox to allow Cart Links to empty cart.
  491. *
  492. * Must be logged in with 'administer cart links' permission.
  493. *
  494. * @param $state
  495. * TRUE to display product action messages, FALSE to not display.
  496. * Defaults to TRUE.
  497. */
  498. function setCartLinksUIAllowEmptying($state = TRUE) {
  499. $this->drupalPost(
  500. 'admin/store/settings/cart-links',
  501. array('uc_cart_links_empty' => $state),
  502. t('Save configuration')
  503. );
  504. $this->assertFieldByName(
  505. 'uc_cart_links_empty',
  506. $state,
  507. t('Allow Cart Links to empty carts is @state.', array('@state' => $state ? 'TRUE' : 'FALSE'))
  508. );
  509. }
  510. /**
  511. * Sets messages that can be referenced by a link.
  512. *
  513. * Must be logged in with 'administer cart links' permission.
  514. *
  515. * @param $messages
  516. * String containing user input from a textarea, one message per line.
  517. * Messages have numeric key and text value, separated by '|'.
  518. */
  519. function setCartLinksUIMessages($messages = '') {
  520. $message_string = implode("\n", $messages);
  521. $this->drupalPost(
  522. 'admin/store/settings/cart-links',
  523. array('uc_cart_links_messages' => $message_string),
  524. t('Save configuration')
  525. );
  526. $this->assertFieldByName(
  527. 'uc_cart_links_messages',
  528. $message_string,
  529. t('Cart Links messages contains "@messages".', array('@messages' => $message_string))
  530. );
  531. }
  532. /**
  533. * Sets allowed Cart Links.
  534. *
  535. * Must be logged in with 'administer cart links' permission.
  536. *
  537. * @param $restrictions
  538. * String containing user input from a textarea, one restriction per line.
  539. * Restrictions are valid Cart Links - i.e. relative URLs.
  540. */
  541. function setCartLinksUIRestrictions($restrictions = '') {
  542. $this->drupalPost(
  543. 'admin/store/settings/cart-links',
  544. array('uc_cart_links_restrictions' => $restrictions),
  545. t('Save configuration')
  546. );
  547. $this->assertFieldByName(
  548. 'uc_cart_links_restrictions',
  549. $restrictions,
  550. t('Cart Links restrictions contains "@restrictions".', array('@restrictions' => $restrictions))
  551. );
  552. }
  553. /**
  554. * Sets redirect destination page for invalid Cart Links.
  555. *
  556. * Must be logged in with 'administer cart links' permission.
  557. *
  558. * @param $url
  559. * Relative URL of the destination page for the redirect. Omit leading '/'.
  560. */
  561. function setCartLinksUIRedirect($url = '') {
  562. $this->drupalPost(
  563. 'admin/store/settings/cart-links',
  564. array('uc_cart_links_invalid_page' => $url),
  565. t('Save configuration')
  566. );
  567. $this->assertFieldByName(
  568. 'uc_cart_links_invalid_page',
  569. $url,
  570. t('Cart Links invalid page URL contains "@url".', array('@url' => $url))
  571. );
  572. }
  573. /**
  574. * Create a page with Cart Links in the body.
  575. *
  576. * @param $links
  577. * Array of Cart Links to appear on page.
  578. */
  579. public function createCartLinksPage($links = array()) {
  580. if (!empty($links)) {
  581. $i = 0;
  582. foreach ($links as $link) {
  583. $body['links'][] = array(
  584. 'title' => t('Cart Link #@num', array('@num' => $i++)),
  585. 'href' => $link,
  586. );
  587. }
  588. }
  589. $page = array(
  590. 'type' => 'page', // This is default anyway ...
  591. 'body' => array(LANGUAGE_NONE => array(
  592. array(
  593. 'value' => isset($body) ? theme('links', $body) : $this->randomName(128),
  594. 'format' => filter_default_format(),
  595. )
  596. )),
  597. );
  598. return $this->drupalCreateNode($page);
  599. }
  600. /**
  601. * Creates a product with all attribute types and options.
  602. *
  603. * @param $product_class
  604. * Defaults to FALSE to create a normal product, set to TRUE to
  605. * create a product class instead.
  606. */
  607. public function createCartLinksProduct($product_class = FALSE) {
  608. // Create a product
  609. if ($product_class) {
  610. $product = $this->createProductClass();
  611. }
  612. else {
  613. $product = $this->createProduct();
  614. }
  615. // Create some attributes
  616. for ($i = 0; $i < 5; $i++) {
  617. $attribute = UbercartAttributeTestCase::createAttribute();
  618. $attributes[$attribute->aid] = $attribute;
  619. }
  620. // Add some options, organizing them by aid and oid.
  621. $attribute_aids = array_keys($attributes);
  622. $all_options = array();
  623. foreach ($attribute_aids as $aid) {
  624. for ($i = 0; $i < 3; $i++) {
  625. $option = UbercartAttributeTestCase::createAttributeOption(array('aid' => $aid));
  626. $all_options[$option->aid][$option->oid] = $option;
  627. }
  628. }
  629. // array('required' => TRUE)
  630. // Get the options.
  631. $attribute = uc_attribute_load($attribute->aid);
  632. // Load every attribute we got.
  633. $attributes_with_options = uc_attribute_load_multiple();
  634. // Pick 5 keys to check at random.
  635. $aids = drupal_map_assoc(array_rand($attributes, 3));
  636. // Load the attributes back.
  637. $loaded_attributes = uc_attribute_load_multiple($aids);
  638. // TODO: add attributes of all 4 types
  639. // TODO: create both required and not required attributes
  640. // Add the selected attributes to the product.
  641. foreach ($loaded_attributes as $loaded_attribute) {
  642. uc_attribute_subject_save($loaded_attribute, 'product', $product->nid, TRUE);
  643. }
  644. return $product;
  645. }
  646. /**
  647. * Creates Cart Links pointing to the given product(s).
  648. *
  649. * Links containing many combinations of attributes and options wil be
  650. * returned. Return value is an associative array containing two keys:
  651. * -links: An array of the actual links we're building.
  652. * -data: An array of metadata about the Cart Links so we won't have to try
  653. * to re-construct this information by parsing the link at a later time.
  654. *
  655. * The 'links' and 'data' sub-arrays are both indexed by the keys used in
  656. * the $products array that is passed in as an argument, so these keys may
  657. * be used to lookup the link and metadata for a specific product.
  658. *
  659. * @param $products
  660. * An array of products.
  661. *
  662. * @return
  663. * Array containing Cart Links and link metadata.
  664. */
  665. function createValidCartLinks($products = array()) {
  666. foreach ($products as $key => $product) {
  667. $nid = $product->nid;
  668. $title = $product->title;
  669. $qty = mt_rand(1, 19);
  670. // $link_data will hold meta information about the Cart Links
  671. // so we won't have to try to re-construct this information by
  672. // parsing the link at a later time.
  673. $link_data[$key] = array(
  674. 'nid' => $nid,
  675. 'title' => $title,
  676. 'qty' => $qty,
  677. 'attributes' => array(),
  678. );
  679. // $cart_links will hold the actual links we're building.
  680. // $cart_links and $link_data share the same keys.
  681. $cart_links[$key] = '/cart/add/p' . $nid . '_q' . $qty;
  682. // Loop over attributes, append all attribute/option combos to links
  683. $attributes = uc_product_get_attributes($nid);
  684. foreach ($attributes as $attribute) {
  685. // If this is textfield, radio, or select option, then
  686. // only 1 option allowed. If checkbox, multiple are allowed.
  687. switch ($attribute->display) {
  688. case 0: // textfield
  689. $value = $this->randomName(12); // Textfield
  690. $link_data[$key]['attributes'][$attribute->label][] = $value;
  691. $cart_links[$key] .= '_a' . $attribute->aid . 'o' . $value;
  692. break;
  693. case 1: // select
  694. case 2: // radios
  695. $option = $attribute->options[array_rand($attribute->options)];
  696. $link_data[$key]['attributes'][$attribute->label][] = $option->name;
  697. $cart_links[$key] .= '_a' . $attribute->aid . 'o' . $option->oid;
  698. break;
  699. case 3: // checkboxes
  700. foreach ($attribute->options as $option) {
  701. $link_data[$key]['attributes'][$attribute->label][] = $option->name;
  702. $cart_links[$key] .= '_a' . $attribute->aid . 'o' . $option->oid;
  703. }
  704. break;
  705. }
  706. }
  707. }
  708. return array('links' => $cart_links, 'data' => $link_data);
  709. }
  710. }