Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
shoppingcartTest.php
1 <?php
2 /**
3  * Created by JetBrains PhpStorm.
4  * User: Andrey
5  * Date: 5/24/13
6  * Time: 12:05 PM
7  * To change this template use File | Settings | File Templates.
8  */
9 
10 class ShoppingcartTest extends CDbTestCase{
11 
12  public $fixtures = array(
13 
14  );
15 
16  public $data;
17 
18  public function setUp(){
19 
20  Yii::app()->getModule('shoppingcart')->setImport(array('shoppingcart.controllers.*'));
21 
22  Item::model()->deleteAll();
23  Order::model()->deleteAll();
24  TmpUser::model()->deleteAll();
25  // import the module-level components
26  $c = $this->getMock('ItemController', array('redirect'),array('phpunit'));
27  $c->expects($this->any())
28  ->method('redirect');
29  $c->setAction(new CInlineAction($c, 'urltest'));
30  Yii::app()->setController($c);
31 
32  $am = $this->getMock('AssetsManager',
33  array('publish'),
34  array(),
35  'MocAssetsManager'
36  )->expects($this->any())
37  ->method('publish');;
38 
39  Yii::app()->setComponent('assetManager', array('class'=>'MocAssetsManager'));
40 
41  $this->data = array(
42  'item_id' => 15,
43  'title' => 'Item name',
44  'price' => 99,
45  'admin_email' => 'andrew_@mail.ru',
46  'page_id' => 3,
47  'class' => 'shoppingcart_item',
48  'shoppingcart_link' => '/Content.Node/service/Shopping_Cart.en.html',
49  );
50  }
51 
52  public function testItemWidget(){
53 
54  $itemWidgetHtml = Yii::app()->controller->widget('shoppingcart.widgets.ItemWidget',
55  $this->data, true
56  );
57 
58  preg_match("/Item\[title\]/", $itemWidgetHtml, $match);
59  $this->assertNotEmpty($match, "Title input does not exists");
60  preg_match("/Item\[price\]/", $itemWidgetHtml, $match);
61  $this->assertNotEmpty($match, "Price input does not exists");
62  preg_match("/Item\[admin_email\]/", $itemWidgetHtml, $match);
63  $this->assertNotEmpty($match, "Admin_email input does not exists");
64  preg_match("/Item\[item_id\]/", $itemWidgetHtml, $match);
65  $this->assertNotEmpty($match, "Admin_email input does not exists");
66  preg_match("/Item\[page_id\]/", $itemWidgetHtml, $match);
67  $this->assertNotEmpty($match, "Page_id input does not exists");
68  preg_match('/class="shoppingcart_item"/', $itemWidgetHtml, $match);
69  $this->assertNotEmpty($match, "Class does not specified");
70  preg_match('/class="shoppingcart_item"/', $itemWidgetHtml, $match);
71  $this->assertNotEmpty($match, "Class does not specified");
72  preg_match('/e117a4a93357557f72e437c0eae0c4e4/', $itemWidgetHtml, $match);
73  $this->assertNotEmpty($match, "Checksum is wrong");
74 
75  $item = new Item();
76  $item->setAttributes($this->data, false);
77 
78  $addItemPost = array(
79  'Item' => array(
80  'quantity' => '2',
81  'title' => EncryptHelper::encrypt($this->data['title']),
82  'price' => EncryptHelper::encrypt($this->data['price']),
83  'admin_email' => EncryptHelper::encrypt($this->data['admin_email']),
84  'item_id' => EncryptHelper::encrypt($this->data['item_id']),
85  'page_id' => EncryptHelper::encrypt($this->data['page_id']),
86  'item_referrer' => '',
87  'shoppingcart_link' => '/Content.Node/service/Shopping_Cart.en.html',
88  ),
89  'checksum' => $item->getItemChecksum(),
90  );
91 
92  $_POST = $addItemPost;
93 
94  $_SERVER = array(
95  'REMOTE_ADDR'=> '127.0.0.1',
96  'HTTP_REFERER' => '/Content.Node/service/Shopping_Cart.en.html',
97  'REDIRECT_URL' => 'REDIRECT_URL',
98  'REQUEST_URI' => '/Content.Node/service/Shopping_Cart.en.html',
99  'SCRIPT_FILENAME' => 'index.php',
100  );
101 
102  Yii::app()->controller->actionAdd();
103 
104  $this->assertEquals(1, (Item::model()->count()), 'The item was not added');
105 
106  // Itme list widget
107  $itemListWidgetHtml = Yii::app()->controller->widget('common.modules.shoppingcart.widgets.ItemListWidget',
108  array('title'=>'MY CART',
109  'count'=>5,
110  'class'=>'itemlist',
111  'shoppingcart_link'=>'widgetsTests/shoppingcartCart',
112  'link_items_to_page' => true
113  ), true
114  );
115 
116  preg_match("/".$this->data['title']."/", $itemListWidgetHtml, $match);
117  $this->assertNotEmpty($match, "Added item is absent in the itemListWidget");
118 
119  //Steps emulation
120  $steps = array(1,2,3,4);
121  foreach($steps as $step){
122 
123  switch($step):
124  case 1:
125  $_POST = array(
126  'Item' => array(
127  0 => array(
128  'quantity' => 3
129  )
130  )
131  );
132 
133  continue;
134  case 2:
135 
136  $_POST = array(
137  'TmpUser' => array(
138  'salutation' => 'mr',
139  'firstname' => 'Andrew',
140  'lastname' => 'Voloshin',
141  'birthday' => 'Jan 1, 1970',
142  'company' => 'oberig',
143  'street' => 'street',
144  'zip_code' => 'zipcode',
145  'city' => 'Kiev',
146  'country' => 'Ukraine',
147  'telephone' => '0630202237',
148  'email' => 'Andrew.Voloshin@mail.ru',
149  'memberNo' => '123',
150  'svnr' => '213',
151  )
152  );
153  continue;
154  case 3:
155 
156  $_POST = array(
157  'Order' => array(
158  'shipping_info' => 'Shipping_info',
159  'payment_info' => 'Payment_info',
160  ),
161  'checksum' => md5('Shipping_infoPayment_info'),
162  );
163 
164 
165  continue;
166  case 4:
167 
168  $_POST = array(
169  'Order'=>array(
170  'verifyCode' => ''
171  ));
172 
173  continue;
174 
175 
176  endswitch;
177 
178  $_REQUEST['step'] = $step;
179 
180  $shoppingCartWidgetHtml = Yii::app()->controller->widget('shoppingcart.widgets.ShoppingCartWidget',
181  array(
182  'class'=>'shoppingcart',
183  'shipping_info' => 'some shipping info ...',
184  'payment_info' => 'some payment info ...',
185  'layout' => 'ShoppingCartWidget',
186  'link_items_to_page' => true
187  ), true
188  );
189  }//end for
190 
191  $this->assertEquals(1, (Order::model()->count('ordered = :ordered', array(':ordered' => 1))), 'The order was not added');
192 
193  }
194 }