0010180: Crm test coupled with Tinebase
authorFlávio Gomes da Silva Lisboa <flavio.lisboa@serpro.gov.br>
Thu, 28 Aug 2014 12:10:15 +0000 (09:10 -0300)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 29 Aug 2014 20:50:16 +0000 (22:50 +0200)
 - ACL roles tests were divided between Crm and Tinebase

Change-Id: I3ca5f97235aa435312713171b2f8e974d8afced5
Reviewed-on: https://gerrit.tine20.org/tine20/2864
Tested-by: jenkins user
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Crm/Acl/RolesTest.php [new file with mode: 0644]
tests/tine20/Crm/AllTests.php
tests/tine20/Tinebase/Acl/RolesTest.php

diff --git a/tests/tine20/Crm/Acl/RolesTest.php b/tests/tine20/Crm/Acl/RolesTest.php
new file mode 100644 (file)
index 0000000..2567fa3
--- /dev/null
@@ -0,0 +1,268 @@
+<?php
+/**
+ * Tine 2.0 - http://www.tine20.org
+ *
+ * @package     Tinebase
+ * @subpackage  Acl
+ * @license     http://www.gnu.org/licenses/agpl.html
+ * @copyright   Copyright (c) 2008-2013 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @copyright   Copyright (c) 2014 Serpro (http://www.serpro.gov.br)
+ * @author      Flávio Gomes da Silva Lisboa <flavio.lisboa@serpro.gov.br>
+ */
+
+/**
+ * Test helper
+ */
+require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
+
+/**
+ * Test class for Tinebase_Acl_Roles
+ */
+class Crm_Acl_RolesTest extends TestCase
+{
+    /**
+     * @var array test objects
+     */
+    protected $objects = array();
+    
+    /**
+     * Sets up the fixture.
+     * This method is called before a test is executed.
+     *
+     * @access protected
+     */
+    protected function setUp()
+    {
+        parent::setUp();
+        
+        $this->objects['application'] = Tinebase_Application::getInstance()->getApplicationByName('Crm');
+        $this->objects['user'] = new Tinebase_Model_FullUser(array(
+            'accountLoginName'      => 'tine20phpunit',
+            'accountDisplayName'    => 'tine20phpunit',
+            'accountStatus'         => 'enabled',
+            'accountExpires'        => NULL,
+            'accountPrimaryGroup'   => Tinebase_Group::getInstance()->getGroupByName('Users')->getId(),
+            'accountLastName'       => 'Tine 2.0',
+            'accountFirstName'      => 'PHPUnit',
+            'accountEmailAddress'   => 'phpunit@metaways.de'
+        ));
+        $this->objects['role'] = new Tinebase_Model_Role(array(
+            'id'                    => 10,
+            'name'                  => 'phpunitrole',
+            'description'           => 'test role for phpunit',
+        ));
+        $this->objects['role_2'] = new Tinebase_Model_Role(array(
+            'id'                    => 11,
+            'name'                  => 'phpunitrole 2',
+            'description'           => 'test role 2 for phpunit',
+        ));
+        
+        // add account for group / role member tests
+        $this->objects['user'] = Tinebase_User::getInstance()->addUser($this->objects['user']);
+        Tinebase_Group::getInstance()->addGroupMember($this->objects['user']->accountPrimaryGroup, $this->objects['user']);
+        
+        return;
+    }
+
+    /**
+     * Tears down the fixture
+     * This method is called after a test is executed.
+     *
+     * @access protected
+     */
+    protected function tearDown()
+    {
+        Tinebase_User::getInstance()->deleteUser($this->objects['user']->accountId);
+        
+        parent::tearDown();
+    }
+
+    /**
+     * try to add a role
+     *
+     */
+    public function testCreateRole()
+    {
+        $role = Tinebase_Acl_Roles::getInstance()->createRole($this->objects['role']);
+        
+        $this->assertEquals($role->getId(), $this->objects['role']->getId());
+    }
+    
+    /**
+     * try to add a role membership
+     *
+     */
+    public function testSetRoleMember()
+    {
+        $this->testCreateRole();
+        
+        $member = array(
+            array(
+                "type"  => 'user',
+                "id"    => $this->objects['user']->getId(),
+            )
+        );
+        Tinebase_Acl_Roles::getInstance()->setRoleMembers($this->objects['role']->getId(), $member);
+        
+        $members = Tinebase_Acl_Roles::getInstance()->getRoleMembers($this->objects['role']->getId());
+        
+        $this->assertGreaterThan(0, count($members));
+    }
+    
+    /**
+     * try to remove member from role
+     */
+    public function removeRoleMember()
+    {
+        $this->testCreateRole();
+        
+        Tinebase_Acl_Roles::getInstance()->removeRoleMember(10, $this->objects['user']);
+        
+        $members = Tinebase_Acl_Roles::getInstance()->getRoleMembers($this->objects['role']->getId());
+        
+        $this->assertEquals(0, count($members));
+    }
+    
+    /**
+     * try to add member to role
+     */
+    public function testAddRoleMember()
+    {
+        $this->testCreateRole();
+        
+        Tinebase_Acl_Roles::getInstance()->addRoleMember($this->objects['role']->getId(), array(
+            'type'     => 'user',
+            'id'    => $this->objects['user']->getId()
+        ));
+        
+        $members = Tinebase_Acl_Roles::getInstance()->getRoleMembers($this->objects['role']->getId());
+        
+        $this->assertGreaterThan(0, count($members));
+    }
+        
+    /**
+     * try to add member to multiple roles
+     */
+    public function testSetRoleMemberships()
+    {
+        $this->testCreateRole();
+        
+        // create role 2 for test
+        $role2 = Tinebase_Acl_Roles::getInstance()->createRole($this->objects['role_2']);
+        
+        // remove role members
+        Tinebase_Acl_Roles::getInstance()->setRoleMembers($this->objects['role']->getId(), array());
+        Tinebase_Acl_Roles::getInstance()->setRoleMembers($this->objects['role_2']->getId(), array());
+        
+        Tinebase_Acl_Roles::getInstance()->setRoleMemberships(
+            array(
+                'type'     => 'user',
+                'id'     => $this->objects['user']->getId()
+            ),
+            array(
+                $this->objects['role']->getId(),
+                $this->objects['role_2']->getId()
+            )
+        );
+        
+        $members = Tinebase_Acl_Roles::getInstance()->getRoleMembers($this->objects['role']->getId());
+        $members_2 = Tinebase_Acl_Roles::getInstance()->getRoleMembers($this->objects['role_2']->getId());
+        
+        $this->assertEquals(2, (count($members) + count($members_2)));
+    }
+    
+    /**
+     * try to add a role right
+     *
+     */
+    public function testSetRoleRight()
+    {
+        $this->testCreateRole();
+        
+        $right = array(
+            array(
+                "application_id"    => $this->objects['application']->getId(),
+                "right"             => Tinebase_Acl_Rights::RUN,
+            )
+        );
+        Tinebase_Acl_Roles::getInstance()->setRoleRights($this->objects['role']->getId(), $right);
+        
+        $rights = Tinebase_Acl_Roles::getInstance()->getRoleRights($this->objects['role']->getId());
+        
+        $this->assertGreaterThan(0, count($rights));
+    }
+    
+    /**
+     * try to check getting applications
+     */
+    public function testGetApplications()
+    {
+        $result = Tinebase_Acl_Roles::getInstance()->getApplications($this->objects['user']->getId());
+        
+        $this->assertGreaterThan(0, count($result->toArray()));
+    }
+
+    /**
+     * try to check getting applications
+     */
+    public function testGetApplicationRights()
+    {
+        $result = Tinebase_Acl_Roles::getInstance()->getApplicationRights(
+            $this->objects['application']->name,
+            $this->objects['user']->getId()
+        );
+        
+        $this->assertGreaterThan(0, count($result));
+    }
+    
+    /**
+     * try to check if user with a role has right
+     */
+    public function testHasRight()
+    {
+        $result = Tinebase_Acl_Roles::getInstance()->hasRight(
+            $this->objects['application']->name,
+            $this->objects['user']->getId(),
+            Tinebase_Acl_Rights::RUN
+        );
+        
+        $this->assertTrue($result, 'has no run right');
+        
+        $result = Tinebase_Acl_Roles::getInstance()->hasRight(
+            $this->objects['application']->name,
+            $this->objects['user']->getId(),
+            Tinebase_Acl_Rights::ADMIN
+        );
+
+        $this->assertFalse($result, 'has admin right for application ' . $this->objects['application']->name);
+        
+        // test for not installed application
+        $result = Tinebase_Acl_Roles::getInstance()->hasRight(
+            'FooBar',
+            $this->objects['user']->getId(),
+            Tinebase_Acl_Rights::ADMIN
+        );
+
+        $this->assertFalse($result, 'user has admin right for not installed application');
+    }
+
+    /**
+     * try to delete a role
+     *
+     */
+    public function testDeleteRole()
+    {
+        $this->testSetRoleMemberships();
+        
+        // remove role members and rights first
+        Tinebase_Acl_Roles::getInstance()->setRoleRights($this->objects['role']->getId(), array());
+        
+        Tinebase_Acl_Roles::getInstance()->setRoleMembers($this->objects['role']->getId(), array());
+        Tinebase_Acl_Roles::getInstance()->setRoleMembers($this->objects['role_2']->getId(), array());
+        
+        Tinebase_Acl_Roles::getInstance()->deleteRoles(array($this->objects['role']->getId(), $this->objects['role_2']->getId()));
+      
+        $this->setExpectedException('Tinebase_Exception_NotFound');
+        Tinebase_Acl_Roles::getInstance()->getRoleById($this->objects['role']->getId());
+    }
+}
index a724554..e1c5e51 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /**
  * Tine 2.0 - http://www.tine20.org
- * 
+ *
  * @package     Crm
  * @license     http://www.gnu.org/licenses/agpl.html
  * @copyright   Copyright (c) 2008 Metaways Infosystems GmbH (http://www.metaways.de)
@@ -32,6 +32,7 @@ class Crm_AllTests
         $suite->addTestSuite('Crm_ControllerTest');
         $suite->addTestSuite('Crm_JsonTest');
         $suite->addTestSuite('Crm_NotificationsTests');
+        $suite->addTestSuite('Crm_Acl_RolesTest');
         return $suite;
     }
 }
index c20da6e..33f37a2 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /**
  * Tine 2.0 - http://www.tine20.org
- * 
+ *
  * @package     Tinebase
  * @subpackage  Acl
  * @license     http://www.gnu.org/licenses/agpl.html
@@ -34,7 +34,7 @@ class Tinebase_Acl_RolesTest extends TestCase
     {
         parent::setUp();
         
-        $this->objects['application'] = Tinebase_Application::getInstance()->getApplicationByName('Crm');
+        $this->objects['application'] = Tinebase_Application::getInstance()->getApplicationByName('Addressbook');
         $this->objects['user'] = new Tinebase_Model_FullUser(array(
             'accountLoginName'      => 'tine20phpunit',
             'accountDisplayName'    => 'tine20phpunit',
@@ -106,7 +106,7 @@ class Tinebase_Acl_RolesTest extends TestCase
         $members = Tinebase_Acl_Roles::getInstance()->getRoleMembers($this->objects['role']->getId());
         
         $this->assertGreaterThan(0, count($members));
-    }    
+    }
     
     /**
      * try to remove member from role
@@ -155,11 +155,11 @@ class Tinebase_Acl_RolesTest extends TestCase
         
         Tinebase_Acl_Roles::getInstance()->setRoleMemberships(
             array(
-                'type'     => 'user', 
+                'type'     => 'user',
                 'id'     => $this->objects['user']->getId()
-            ), 
+            ),
             array(
-                $this->objects['role']->getId(), 
+                $this->objects['role']->getId(),
                 $this->objects['role_2']->getId()
             )
         );
@@ -189,7 +189,7 @@ class Tinebase_Acl_RolesTest extends TestCase
         $rights = Tinebase_Acl_Roles::getInstance()->getRoleRights($this->objects['role']->getId());
         
         $this->assertGreaterThan(0, count($rights));
-    }    
+    }
     
     /**
      * try to check getting applications
@@ -207,12 +207,12 @@ class Tinebase_Acl_RolesTest extends TestCase
     public function testGetApplicationRights()
     {
         $result = Tinebase_Acl_Roles::getInstance()->getApplicationRights(
-            $this->objects['application']->name, 
+            $this->objects['application']->name,
             $this->objects['user']->getId()
         );
         
         $this->assertGreaterThan(0, count($result));
-    }    
+    }
     
     /**
      * try to check if user with a role has right
@@ -220,16 +220,16 @@ class Tinebase_Acl_RolesTest extends TestCase
     public function testHasRight()
     {
         $result = Tinebase_Acl_Roles::getInstance()->hasRight(
-            $this->objects['application']->name, 
-            $this->objects['user']->getId(), 
+            $this->objects['application']->name,
+            $this->objects['user']->getId(),
             Tinebase_Acl_Rights::RUN
         );
         
         $this->assertTrue($result, 'has no run right');
         
         $result = Tinebase_Acl_Roles::getInstance()->hasRight(
-            $this->objects['application']->name, 
-            $this->objects['user']->getId(), 
+            $this->objects['application']->name,
+            $this->objects['user']->getId(),
             Tinebase_Acl_Rights::ADMIN
         );
 
@@ -237,13 +237,13 @@ class Tinebase_Acl_RolesTest extends TestCase
         
         // test for not installed application
         $result = Tinebase_Acl_Roles::getInstance()->hasRight(
-            'FooBar', 
-            $this->objects['user']->getId(), 
+            'FooBar',
+            $this->objects['user']->getId(),
             Tinebase_Acl_Rights::ADMIN
         );
 
         $this->assertFalse($result, 'user has admin right for not installed application');
-    }    
+    }
 
     /**
      * try to delete a role