You are here

function UbercartCartLinksTestCase::testCartLinksRestrictions in Ubercart 6.2

Same name and namespace in other branches
  1. 7.3 uc_cart_links/tests/uc_cart_links.test \UbercartCartLinksTestCase::testCartLinksRestrictions()

Tests cart links on a page under a variety of conditions.

File

uc_cart_links/uc_cart_links.test, line 302
Ubercart Cart Links Tests.

Class

UbercartCartLinksTestCase
SimpleTests for Ubercart Cart Links.

Code

function testCartLinksRestrictions() {

  // Create product
  $products[] = $this
    ->createCartLinksProduct(FALSE);

  // Create a product class
  $products[] = $this
    ->createCartLinksProduct(FALSE);

  // later ...
  // Create some valid cart links for these products
  $link_array = $this
    ->createValidCartLinks($products);
  $cart_links = $link_array['links'];
  $link_data = $link_array['data'];

  // Create a page containing these links
  $page = $this
    ->createCartLinksPage($cart_links);
  $this
    ->drupalLogin($this->adminUser);

  //
  // Test cart links restrictions
  //
  // Go to page with cart links
  $this
    ->drupalGet('node/' . $page->nid);

  // Pick one of the links at random and restrict it
  $test_link_0 = array_rand($cart_links);

  // Only this link is allowed - strip '/cart/add/' from beginning
  $this
    ->setCartLinksUIRestrictions(substr($cart_links[$test_link_0], 10));

  // Attempt to click link - should pass
  $this
    ->drupalGet('node/' . $page->nid);
  $this
    ->clickLink(t('Cart Link #@link', array(
    '@link' => $test_link_0,
  )));

  // Check for notice that item was added (this notice is set ON
  // by default, see admin/store/settings/cart/edit)
  $this
    ->assertText(t('@title added to your shopping cart.', array(
    '@title' => $link_data[$test_link_0]['title'],
  )), t('Product @title added to cart.', array(
    '@title' => $link_data[$test_link_0]['title'],
  )));

  // Pick another link at random, as long as it is different from first
  $in_cart = $cart_links[$test_link_0];
  unset($cart_links[$test_link_0]);
  $test_link = array_rand($cart_links);

  // Attempt to click it
  // It should fail and redirect to the home page (default)
  $this
    ->drupalGet('node/' . $page->nid);
  $this
    ->clickLink(t('Cart Link #@link', array(
    '@link' => $test_link,
  )));
  $this
    ->assertNoText(t('@title added to your shopping cart.', array(
    '@title' => $link_data[$test_link]['title'],
  )), t('Product @title added to cart.', array(
    '@title' => $link_data[$test_link]['title'],
  )));
  $this
    ->assertText(t('Welcome to your new Drupal website!'));
  $this
    ->assertText(t('This message will disappear once you have promoted a post to the front page.'), t('Redirected to front page for link not in restrictions.'));

  // Now create a special redirect page for bad links
  $redirect_page = $this
    ->drupalCreateNode(array(
    'body' => 'ERROR: Invalid Cart Link!',
    'format' => FILTER_FORMAT_DEFAULT,
  ));

  // Set redirect link
  $this
    ->setCartLinksUIRedirect('node/' . $redirect_page->nid);

  // Attempt to click same restricted link as above.
  // It should fail again but this time redirect to $redirect_page.
  $this
    ->drupalGet('node/' . $page->nid);
  $this
    ->clickLink(t('Cart Link #@link', array(
    '@link' => $test_link,
  )));
  $this
    ->assertText(t('ERROR: Invalid Cart Link!'), t('Redirected to error page for link not in restrictions.'));

  // Remove restrictions, try to add again - it should pass
  $this
    ->setCartLinksUIRestrictions('');
  $this
    ->drupalGet('node/' . $page->nid);
  $this
    ->clickLink(t('Cart Link #@link', array(
    '@link' => $test_link,
  )));
  $this
    ->assertText(t('@title added to your shopping cart.', array(
    '@title' => $link_data[$test_link]['title'],
  )), t('Product @title added to cart.', array(
    '@title' => $link_data[$test_link]['title'],
  )));
  $this
    ->drupalLogout();
}