Commit 53633a2d authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

! New test helpers implementation

parent 9f4cc5a2
...@@ -27,6 +27,7 @@ use EcomDev_PHPUnit_Helper as TestHelper; ...@@ -27,6 +27,7 @@ use EcomDev_PHPUnit_Helper as TestHelper;
* @method EcomDev_PHPUnit_Mock_Proxy mockBlock($classAlias, array $methods = array(), array $constructorArgs = array()) * @method EcomDev_PHPUnit_Mock_Proxy mockBlock($classAlias, array $methods = array(), array $constructorArgs = array())
* @method EcomDev_PHPUnit_Mock_Proxy mockHelper($classAlias, array $methods = array(), array $constructorArgs = array()) * @method EcomDev_PHPUnit_Mock_Proxy mockHelper($classAlias, array $methods = array(), array $constructorArgs = array())
* @method EcomDev_PHPUnit_Mock_Proxy mockSession($classAlias, array $methods = array()) * @method EcomDev_PHPUnit_Mock_Proxy mockSession($classAlias, array $methods = array())
* @method EcomDev_PHPUnit_Mock_Proxy adminSession(array $resources = array())
*/ */
abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
{ {
......
<?php <?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
use EcomDev_PHPUnit_Helper as TestHelper;
use EcomDev_PHPUnit_Test_Case_Util as TestUtil;
/**
* Helper for stubbing customer session
*
*
*/
class EcomDev_PHPUnit_Test_Case_Helper_Customer extends EcomDev_PHPUnit_Helper_Abstract class EcomDev_PHPUnit_Test_Case_Helper_Customer extends EcomDev_PHPUnit_Helper_Abstract
{ {
/** /**
...@@ -12,7 +36,11 @@ class EcomDev_PHPUnit_Test_Case_Helper_Customer extends EcomDev_PHPUnit_Helper_A ...@@ -12,7 +36,11 @@ class EcomDev_PHPUnit_Test_Case_Helper_Customer extends EcomDev_PHPUnit_Helper_A
*/ */
public function helperCustomerSession($customerId, $storeId = null) public function helperCustomerSession($customerId, $storeId = null)
{ {
$customerSessionMock = $this->helperMockSession('customer/session', array('renewSession')); $customerSessionMock = TestHelper::invoke(
'mockSession',
'customer/session',
array('renewSession')
);
if ($storeId === null) { if ($storeId === null) {
$storeId = TestUtil::app()->getAnyStoreView()->getCode(); $storeId = TestUtil::app()->getAnyStoreView()->getCode();
......
...@@ -18,6 +18,10 @@ ...@@ -18,6 +18,10 @@
use EcomDev_PHPUnit_Test_Case_Util as TestUtil; use EcomDev_PHPUnit_Test_Case_Util as TestUtil;
/**
* Session mocks helper
*
*/
class EcomDev_PHPUnit_Test_Case_Helper_Session class EcomDev_PHPUnit_Test_Case_Helper_Session
extends EcomDev_PHPUnit_Helper_Abstract extends EcomDev_PHPUnit_Helper_Abstract
implements EcomDev_PHPUnit_Helper_Listener_Interface implements EcomDev_PHPUnit_Helper_Listener_Interface
...@@ -57,10 +61,17 @@ class EcomDev_PHPUnit_Test_Case_Helper_Session ...@@ -57,10 +61,17 @@ class EcomDev_PHPUnit_Test_Case_Helper_Session
$session = $this->helperMockSession('admin/session', array('refreshAcl')); $session = $this->helperMockSession('admin/session', array('refreshAcl'));
$user = $this->createUser(); $user = $this->createUser();
$this->loadRules($user, $this->getAcl(), $resources); $this->loadRules($user, $this->getAcl(), $resources);
$session->setAcl($this->getAcl());
$session->setUser($user); $session->setUser($user);
return $session; return $session;
} }
/**
* Returns an instance of ACL object that will be used for all
* admin stubs
*
* @return Mage_Admin_Model_Acl
*/
public function getAcl() public function getAcl()
{ {
if ($this->acl === null) { if ($this->acl === null) {
...@@ -70,42 +81,69 @@ class EcomDev_PHPUnit_Test_Case_Helper_Session ...@@ -70,42 +81,69 @@ class EcomDev_PHPUnit_Test_Case_Helper_Session
return $this->acl; return $this->acl;
} }
public function loadRules($user, $acl, array $allowedResources = array()) /**
* Loads role rules into ACL for admin user
*
* @param Mage_Admin_Model_User $user
* @param Mage_Admin_Model_Acl $acl
* @param array $allowedResources
*
* @return $this
*/
public function loadRules(Mage_Admin_Model_User $user, Mage_Admin_Model_Acl $acl, array $allowedResources = array())
{ {
$userRole = Mage::getModel('admin/acl_role_user', Mage_Admin_Model_Acl::ROLE_TYPE_USER . $user->getId()); $userRole = Mage::getModel('admin/acl_role_user', Mage_Admin_Model_Acl::ROLE_TYPE_USER . $user->getId());
$acl->addRole($userRole); $acl->addRole($userRole);
if (empty($allowedResources)) { if (empty($allowedResources)) {
$acl->allow($userRole); $acl->allow($userRole);
$acl->allow($userRole, $acl->getResources());
return $this; return $this;
} }
$aclResources = $acl->getResources();
$allow = array(); $allow = array();
foreach ($allowedResources as $resource) { foreach ($allowedResources as $resource) {
$childResources = array_filter( $childResources = array_filter(
$acl->getResources(), $aclResources,
function ($entry) use ($resource) { function ($entry) use ($resource) {
return strpos($entry, 'admin/' . $resource) === 0; return strpos($entry, 'admin/' . $resource) === 0;
} }
); );
$allow = array_merge($allow, array($resource), $childResources); $allow = array_merge($allow, $childResources);
} }
$deny = array(); $deny = array();
foreach ($acl->getResources() as $resource) { foreach ($aclResources as $resource) {
if (!in_array($resource, $allow)) { if (!in_array($resource, $allow)) {
$deny[] = $resource; $deny[] = $resource;
} }
} }
$deny; $acl->allow($userRole, $allow);
$acl->deny($userRole, $deny);
return $this;
} }
/**
* Creates a new instance of user with unique id
*
* Used for stubbing admin user roles
*
* @param int $entropy
* @return Mage_Admin_Model_User
*/
public function createUser($entropy = 3) public function createUser($entropy = 3)
{ {
$userId = floor(microtime(true)*pow(10, $entropy) - floor(time()/100)*100*pow(10, $entropy)); $userId = floor(microtime(true)*pow(10, $entropy) - floor(time()/100)*100*pow(10, $entropy));
return Mage::getModel('admin/user')->setId($userId); // Fix for EE gws functionality
$userRole = Mage::getModel('admin/roles');
$userRole->setGwsIsAll(1);
$user = Mage::getModel('admin/user');
$user->setId($userId);
EcomDev_Utils_Reflection::setRestrictedPropertyValue($user, '_role', $userRole);
return $user;
} }
/** /**
......
...@@ -145,6 +145,7 @@ ...@@ -145,6 +145,7 @@
<helpers> <helpers>
<mock>EcomDev_PHPUnit_Test_Case_Helper_Mock</mock> <mock>EcomDev_PHPUnit_Test_Case_Helper_Mock</mock>
<session>EcomDev_PHPUnit_Test_Case_Helper_Session</session> <session>EcomDev_PHPUnit_Test_Case_Helper_Session</session>
<customer>EcomDev_PHPUnit_Test_Case_Helper_Customer</customer>
</helpers> </helpers>
</suite> </suite>
</phpunit> </phpunit>
......
<?php <?php
/**
* Tests for customer session creation
*
* @loadSharedFixture customers
*/
class EcomDev_PHPUnitTest_Test_Helper_Customer extends EcomDev_PHPUnit_Test_Case class EcomDev_PHPUnitTest_Test_Helper_Customer extends EcomDev_PHPUnit_Test_Case
{ {
/**
*
* @dataProvider dataProvider
*/
public function testCustomerSession($customerId)
{
$customerSession = $this->customerSession($customerId);
$expected = $this->expected('auto');
$this->assertEquals($expected->getIsLoggedIn(), $customerSession->isLoggedIn());
if ($expected->getName()) {
$this->assertEquals($expected->getName(), $customerSession->getCustomer()->getName());
}
}
} }
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
class EcomDev_PHPUnitTest_Test_Helper_Session extends EcomDev_PHPUnit_Test_Case_Controller class EcomDev_PHPUnitTest_Test_Helper_Session extends EcomDev_PHPUnit_Test_Case_Controller
{ {
/**
* Tests stubbing of any session
*
*/
public function testMockSession() public function testMockSession()
{ {
$sessionMock = $this->mockSession('admin/session', array('getUserId')); $sessionMock = $this->mockSession('admin/session', array('getUserId'));
...@@ -19,7 +23,8 @@ class EcomDev_PHPUnitTest_Test_Helper_Session extends EcomDev_PHPUnit_Test_Case_ ...@@ -19,7 +23,8 @@ class EcomDev_PHPUnitTest_Test_Helper_Session extends EcomDev_PHPUnit_Test_Case_
public function testAdminSessionAllRights() public function testAdminSessionAllRights()
{ {
$this->adminSession(); $this->adminSession();
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('catalog/product')); $this->assertTrue(Mage::getSingleton('admin/session')->isLoggedIn());
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('catalog/products'));
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('sales/order')); $this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('sales/order'));
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('system/config')); $this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('system/config'));
} }
...@@ -31,9 +36,9 @@ class EcomDev_PHPUnitTest_Test_Helper_Session extends EcomDev_PHPUnit_Test_Case_ ...@@ -31,9 +36,9 @@ class EcomDev_PHPUnitTest_Test_Helper_Session extends EcomDev_PHPUnit_Test_Case_
public function testAdminSessionOnlyCatalog() public function testAdminSessionOnlyCatalog()
{ {
$this->adminSession(array('catalog')); $this->adminSession(array('catalog'));
$this->assertTrue(Mage::getSingleton('admin/session')->isLoggedIn());
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('catalog/product')); $this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('catalog/products'));
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('catalog/category')); $this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('catalog/categories'));
$this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('sales/order')); $this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('sales/order'));
$this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('system/config')); $this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('system/config'));
} }
...@@ -44,10 +49,11 @@ class EcomDev_PHPUnitTest_Test_Helper_Session extends EcomDev_PHPUnit_Test_Case_ ...@@ -44,10 +49,11 @@ class EcomDev_PHPUnitTest_Test_Helper_Session extends EcomDev_PHPUnit_Test_Case_
*/ */
public function testAdminSessionOnlyCatalogProduct() public function testAdminSessionOnlyCatalogProduct()
{ {
$this->adminSession(array('catalog/product')); $this->adminSession(array('catalog/products'));
$this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('catalog/product')); $this->assertTrue(Mage::getSingleton('admin/session')->isLoggedIn());
$this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('catalog/category')); $this->assertTrue(Mage::getSingleton('admin/session')->isAllowed('catalog/products'));
$this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('catalog/categories'));
$this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('sales/order')); $this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('sales/order'));
$this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('system/config')); $this->assertFalse(Mage::getSingleton('admin/session')->isAllowed('system/config'));
} }
......
john_doe:
- 1
jane_doe:
- 2
unknown_customer:
- 3
\ No newline at end of file
john_doe:
name: John Doe
is_logged_in: true
jane_doe:
name: Jane Doe
is_logged_in: true
unknown_customer:
is_logged_in: false
\ No newline at end of file
eav:
customer:
- entity_id: 1 # Customer with default billing and shipping addresses
attribute_set_id: 0 # Fix issue with customer entity
website_id: 1
store_id: 1
created_in: Default Store
group_id: 1
firstname: John
lastname: Doe
email: john@doe.com
pasword_hash: d1bcc360d05a6bdd7484784f43660c88:hG
default_billing: 1
default_shipping: 1
- entity_id: 2 # Customer without addresses
attribute_set_id: 0 # Fix issue with customer entity
website_id: 1
store_id: 1
created_in: Default Store
group_id: 1
firstname: Jane
lastname: Doe
email: jane@doe.com
pasword_hash: d1bcc360d05a6bdd7484784f43660c88:hG
customer_address:
- entity_id: 1 # Let him to be a guy from Nevada
attribute_set_id: 0 # Fix issue with customer address entity
customer_id: 1
parent_id: 1
firstname: John
lastname: Doe
country_id: US
region_id: 39
region_code: NV
region: Nevada
postcode: 89001
city: Alamo
telephone: 555-55-55
street: Address 123
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment