uc_cart_links.test 26 KB

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