/**
  * tests demo data creation for all applications having demodata prepared
  */
 public function testCreateAllDemoData()
 {
     $adbController = Addressbook_Controller_Contact::getInstance();
     $normalContactsFilter = new Addressbook_Model_ContactFilter(array(array('field' => 'type', 'operator' => 'not', 'value' => 'user')));
     $existingContacts = $adbController->search($normalContactsFilter);
     // skip admin as it will be called before all tests
     $opts = new Zend_Console_Getopt('abp:', array('skipAdmin'));
     ob_start();
     $this->_cli->createAllDemoData($opts);
     ob_end_clean();
     // test addressbook contacts / admin user contacts
     $accountContactsFilter = new Addressbook_Model_ContactFilter(array(array('field' => 'type', 'operator' => 'equals', 'value' => 'user')));
     $normalContactsFilter = new Addressbook_Model_ContactFilter(array(array('field' => 'type', 'operator' => 'equals', 'value' => 'contact')));
     $normalContacts = $adbController->search($normalContactsFilter);
     $accountContacts = $adbController->search($accountContactsFilter);
     // shared should be 700
     $this->assertEquals(700 + $existingContacts->count(), $normalContacts->count(), 'NormalContacts');
     // internal contacts/accounts
     $this->assertEquals(6, $accountContacts->count(), 'AccountContacts');
     // test calendar entries
     $calController = Calendar_Controller_Event::getInstance();
     $allEventsFilter = new Calendar_Model_EventFilter(array());
     $allEvents = $calController->search($allEventsFilter);
     $this->assertEquals(49, $allEvents->count(), 'Events');
     // test crm leads
     $crmController = Crm_Controller_Lead::getInstance();
     $allLeadsFilter = new Crm_Model_LeadFilter(array());
     $allLeads = $crmController->search($allLeadsFilter);
     $this->assertEquals(1, $allLeads->count(), 'One shared lead should have been found');
     // test human resources
     $employeeController = HumanResources_Controller_Employee::getInstance();
     $allEmployeesFilter = new HumanResources_Model_EmployeeFilter(array());
     $allEmployees = $employeeController->search($allEmployeesFilter);
     $this->assertEquals(6, $allEmployees->count());
     // remove employees again
     $employeeController->delete($allEmployees->id);
 }
 /**
  * reinstall applications
  * and reset Demodata
  * php setup.php --reset_demodata USERNAME
  * 
  * @param Zend_Console_Getopt $_opts
  */
 protected function _resetDemodata(Zend_Console_Getopt $_opts)
 {
     $controller = Setup_Controller::getInstance();
     $userController = Admin_Controller_User::getInstance();
     $containerController = Tinebase_Container::getInstance();
     $cli = new Tinebase_Frontend_Cli();
     //Don't reset this applications
     $fixedApplications = array('Tinebase', 'Admin', 'Addressbook');
     //Log in
     $opts = $_opts->getRemainingArgs();
     $username = $opts[0];
     if (empty($username)) {
         echo "Username is missing!\n";
         exit;
     }
     $user = Tinebase_User::getInstance()->getUserByLoginName($username);
     Tinebase_Core::set(Tinebase_Core::USER, $user);
     //get all applications and remove some
     $applications = Tinebase_Application::getInstance()->getApplications(NULL, 'id');
     foreach ($applications as $key => &$application) {
         if (in_array($application, $fixedApplications)) {
             unset($applications[$key]);
         }
     }
     //get set rights
     $users = Tinebase_Acl_Roles::getInstance()->getRoleByName('user role');
     $rights = Tinebase_Acl_Roles::getInstance()->getRoleRights($users->getId());
     //Uninstall Applications
     try {
         $controller->uninstallApplications($applications->name);
         echo "Successfully uninstalled " . count($applications) . " applications.\n";
     } catch (Tinebase_Exception_NotFound $e) {
     }
     //Install Applications
     try {
         $controller->installApplications($applications->name);
         echo "Successfully installed " . count($applications) . " applications.\n";
     } catch (Tinebase_Exception_NotFound $e) {
     }
     //set rights
     foreach ($applications as &$application) {
         $newApplicationId = Tinebase_Application::getInstance()->getApplicationByName($application->name)->getId();
         foreach ($rights as &$right) {
             if ($right['application_id'] == $application->id) {
                 $right['application_id'] = $newApplicationId;
             }
         }
     }
     Tinebase_Acl_Roles::getInstance()->setRoleRights($users->getId(), $rights);
     echo "Successfully restored user rights.\n";
     //Clean up addressbooks
     $internalContacts = $userController->getDefaultInternalAddressbook();
     $containers = $containerController->getAll();
     foreach ($containers as $key => &$container) {
         if ($container->id == $internalContacts) {
             // Do nothing
         } else {
             try {
                 $containerController->deleteContainer($container, true);
             } catch (Tinebase_Exception_NotFound $e) {
             }
         }
     }
     echo "Successfully cleand up containers.\n";
     //remove state
     $db = Tinebase_Core::getDb();
     $statement = "TRUNCATE TABLE " . $db->quoteIdentifier(SQL_TABLE_PREFIX . 'state');
     $db->query($statement);
     echo "Successfully truncated state table.\n";
     //Get Demodata
     $cli->createAllDemoData();
     //clear Cache
     Tinebase_Core::getCache()->clean(Zend_Cache::CLEANING_MODE_ALL);
     echo "Successfully cleared Cache.\n";
     echo "Every thing done!\n";
 }