Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-mw committed Jun 8, 2023
1 parent bf606f8 commit 6ed7ebf
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/MicroweberPackages/Cart/CartManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public function update_cart($data)
if ($cont == false) {
return array('error' => 'Invalid product?');
} else {
if (is_array($cont) and isset($cont['title'])) {
if (is_array($cont) and isset($cont['title']) and $cont['title']) {
$data['title'] = $cont['title'];
}
}
Expand Down Expand Up @@ -779,6 +779,10 @@ public function update_cart($data)
$cart['allow_html'] = 1;
$cart['price'] = doubleval($found_price);
$cart['limit'] = 1;
if(!isset($data['title']) or $data['title'] == false){
$data['title'] = 'Product '.$cart['rel_id'];
}

$cart['title'] = mw()->format->clean_html($data['title']);

$cart_return['custom_fields_data'] = $add;
Expand Down
60 changes: 53 additions & 7 deletions src/MicroweberPackages/Customer/tests/CustomerCheckoutTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

namespace MicroweberPackages\Customer\tests;

use MicroweberPackages\Core\tests\TestCase;
use MicroweberPackages\Checkout\CheckoutManager;
use MicroweberPackages\Core\tests\TestCase;
use MicroweberPackages\Customer\Models\Address;
use MicroweberPackages\Customer\Models\Customer;
use MicroweberPackages\Utils\Mail\MailSender;

/**
* Run test
* @author Bobi Microweber
* @command php phpunit.phar --filter CheckoutTest
*/

class CustomerCheckoutTest extends TestCase
{
public static $content_id = 1;
Expand All @@ -37,7 +36,7 @@ private function _addProductToCart($title)

$saved_id = save_content($params);
$prices_data = app()->shop_manager->get_product_prices($saved_id, false);
$this->assertEquals($prices_data['Price'],$productPrice);
$this->assertEquals($prices_data['Price'], $productPrice);


$get = get_content_by_id($saved_id);
Expand All @@ -50,7 +49,7 @@ private function _addProductToCart($title)
'price' => $productPrice,
);
$cart_add = update_cart($add_to_cart);

$this->assertEquals(isset($cart_add['success']), true);
$this->assertEquals(isset($cart_add['product']), true);
$this->assertEquals($cart_add['product']['price'], $productPrice);
Expand All @@ -59,7 +58,7 @@ private function _addProductToCart($title)
public function testCheckout()
{

\Config::set('mail.transport', 'array');
\Config::set('mail.transport', 'array');

$this->_addProductToCart('Product 1');
$this->_addProductToCart('Product 2');
Expand Down Expand Up @@ -109,7 +108,54 @@ public function testCheckout()
$this->assertEquals($address->state, $checkoutDetails['state']);



}

public function testCheckoutCustomerWithXss()
{

$this->_addProductToCart('Product 1');
$this->_addProductToCart('Product 2');
$this->_addProductToCart('Product 3');
$this->_addProductToCart('Product 4');
$xss = '<style>@keyframes x{}</style><xss style="animation-name:x" onanimationend="alert(document.cookie)"></xss>';
$checkoutDetails = array();
$checkoutDetails['email'] = 'client_' . uniqid() . '@microweber.com';
$checkoutDetails['first_name'] = 'Client First Name<script>alert(1)</script>' . $xss;
$checkoutDetails['last_name'] = 'Microweber Last Name' . $xss;
$checkoutDetails['phone'] = '08812345' . rand(100, 999) . $xss;
$checkoutDetails['address'] = 'Business Park, Mladost 4' . $xss;
$checkoutDetails['address2'] = 'Business Park, Mladost 6' . $xss;
$checkoutDetails['city'] = 'Sofia' . $xss;
$checkoutDetails['state'] = 'Sofia City' . $xss;
$checkoutDetails['country'] = 'Bulgaria' . $xss;
$checkoutDetails['zip'] = '1000' . $xss;


$checkout = new CheckoutManager();
$checkoutStatus = $checkout->checkout($checkoutDetails);

$this->assertArrayHasKey('success', $checkoutStatus);
$this->assertArrayHasKey('id', $checkoutStatus);


// Find customer
$customer = Customer::where('email', $checkoutDetails['email'])->first();

$this->assertEquals($customer->email, $checkoutDetails['email']);
$this->assertNotEquals($customer->first_name, $checkoutDetails['first_name']);
$this->assertNotEquals($customer->last_name, $checkoutDetails['last_name']);
$this->assertNotEquals($customer->phone, $checkoutDetails['phone']);

// Find customer
$address = Address::where('customer_id', $customer->id)->first();

$this->assertNotEquals($address->phone, $checkoutDetails['phone']);
$this->assertNotEquals($address->address_street_1, $checkoutDetails['address']);
$this->assertNotEquals($address->address_street_2, $checkoutDetails['address2']);
$this->assertNotEquals($address->city, $checkoutDetails['city']);
$this->assertNotEquals($address->zip, $checkoutDetails['zip']);
$this->assertNotEquals($address->state, $checkoutDetails['state']);


}
}
15 changes: 13 additions & 2 deletions src/MicroweberPackages/Order/OrderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,24 @@ public function delete_order($data)
}
}

public function place_order($place_order)
public function place_order($place_order = array())
{
$sid = mw()->user_manager->session_id();
if ($sid == false) {
return $sid;
}

if (empty($place_order)) {
return;
}
array_walk_recursive(
$place_order,
function(&$string) {
if (is_string($string)) {
$string = trim(strip_tags($string));
}
}
);
$place_order = xss_clean($place_order);
event($event = new OrderIsCreating($place_order));
$should_mark_as_paid = false;
$place_order = array_filter($place_order);
Expand Down
98 changes: 98 additions & 0 deletions src/MicroweberPackages/Shop/tests/CheckoutClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
namespace MicroweberPackages\Shop\tests;

use MicroweberPackages\Checkout\CheckoutManager;
use MicroweberPackages\Core\tests\TestCase;


class CheckoutClientTest extends TestCase
{
public static $content_id = 1;

private function _addProductToCart($title)
{
app()->database_manager->extended_save_set_permission(true);

$productPrice = rand(1, 4444);

$params = array(
'title' => $title,
'content_type' => 'product',
'subtype' => 'product',
'custom_fields_advanced' => array(
array('type' => 'dropdown', 'name' => 'Color', 'value' => array('Purple', 'Blue')),
array('type' => 'price', 'name' => 'Price', 'value' => $productPrice),

),
'is_active' => 1,);


$saved_id = save_content($params);
$get = get_content_by_id($saved_id);

$this->assertEquals($saved_id, ($get['id']));
self::$content_id = $saved_id;

$add_to_cart = array(
'content_id' => self::$content_id,
'price' => $productPrice,
);
$cart_add = update_cart($add_to_cart);

$this->assertEquals(isset($cart_add['success']), true);
$this->assertEquals(isset($cart_add['product']), true);
$this->assertEquals($cart_add['product']['price'], $productPrice);
}

public function testCheckoutClientNames()
{
empty_cart();


$this->_addProductToCart('CheckoutClientTestProduct 1');
$this->_addProductToCart('CheckoutClientTestProduct 2');
$this->_addProductToCart('CheckoutClientTestProduct 3');
$this->_addProductToCart('CheckoutClientTestProduct 4');

$email = 'client+'.uniqid('testCheckoutClientNames').'test@microweber.com';
$checkoutDetails = array();
$checkoutDetails['email'] = $email;
$checkoutDetails['first_name'] = 'Client';
$checkoutDetails['last_name'] = 'Microweber';
$checkoutDetails['phone'] = '08812345678';
$checkoutDetails['address'] = 'Business Park, Mladost 4';
$checkoutDetails['city'] = 'Sofia';
$checkoutDetails['state'] = 'Sofia City';
$checkoutDetails['country'] = 'Bulgaria';
$checkoutDetails['zip'] = '1000';

$checkout = new CheckoutManager();
$checkoutStatus = $checkout->checkout($checkoutDetails);

$this->assertArrayHasKey('success', $checkoutStatus);
$this->assertArrayHasKey('id', $checkoutStatus);
$this->assertArrayHasKey('order_completed', $checkoutStatus);
$this->assertArrayHasKey('amount', $checkoutStatus);
$this->assertArrayHasKey('currency', $checkoutStatus);
$this->assertArrayHasKey('order_status', $checkoutStatus);

$this->assertEquals($checkoutStatus['order_completed'], 1);
$this->assertEquals($checkoutStatus['first_name'], $checkoutDetails['first_name']);
$this->assertEquals($checkoutStatus['last_name'], $checkoutDetails['last_name']);
$this->assertEquals($checkoutStatus['email'], $checkoutDetails['email']);
$this->assertEquals($checkoutStatus['country'], $checkoutDetails['country']);
$this->assertEquals($checkoutStatus['city'], $checkoutDetails['city']);
$this->assertEquals($checkoutStatus['state'], $checkoutDetails['state']);
$this->assertEquals($checkoutStatus['zip'], $checkoutDetails['zip']);
$this->assertEquals($checkoutStatus['address'], $checkoutDetails['address']);




}





}
15 changes: 7 additions & 8 deletions src/MicroweberPackages/Shop/tests/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ public function testCheckoutDeletedProduct()
$content_data_after_order = content_data($saved_id);
$this->assertEquals(10, $content_data_after_order['qty']);

$productQuery = \MicroweberPackages\Product\Models\Product::query();
$productQuery = $productQuery->whereHas('orders');
$products = $productQuery->get();
$this->assertTrue($products->isEmpty());

$order = get_order_by_id($checkoutStatus);
$this->assertNotNull($order);
$this->assertNull($order['amount']);

$order = get_order_by_id($checkoutStatus);
$this->assertNotNull($order);
Expand Down Expand Up @@ -342,14 +342,13 @@ public function testCheckoutUnpublishedProduct()
$content_data_after_order = content_data($saved_id);
$this->assertEquals(11, $content_data_after_order['qty']);

$productQuery = \MicroweberPackages\Product\Models\Product::query();
$productQuery = $productQuery->whereHas('orders');
$products = $productQuery->get();
$this->assertTrue($products->isEmpty());


$order = get_order_by_id($checkoutStatus);
$this->assertNotNull($order);
$this->assertNull($order['amount']);

}


}

0 comments on commit 6ed7ebf

Please sign in to comment.