You are here

public function UbercartCartLinksTestCase::testCartLinksRestrictions in Ubercart 7.3

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

Tests Cart Links restrictions.

File

uc_cart_links/tests/uc_cart_links.test, line 305
Ubercart Cart Links Tests.

Class

UbercartCartLinksTestCase
SimpleTests for Ubercart Cart Links.

Code

public 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).
  $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
    ->assertText(t('Welcome to Drupal'));
  $this
    ->assertText(t('No front page content has been created yet.'), 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' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => 'ERROR: Invalid Cart Link!',
        ),
      ),
    ),
  ));

  // 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();
}