Exemplo n.º 1
0
 protected function makeSearchAttributeData()
 {
     $metadataAdapter = new SearchDataProviderMetadataAdapter($this->getSearchModel(), Yii::app()->user->userModel->id, $this->getSearchAttributes());
     $metadata = $metadataAdapter->getAdaptedMetadata();
     $this->resolveSearchAttributesMetadataAgainstStateAdapter($metadata);
     return $metadata;
 }
Exemplo n.º 2
0
 /**
  * Method to help us simulate Controller::makeRedBeanDataProviderByDataCollection method.
  */
 public static function makeRedBeanDataProvider($searchModel, $listModelClassName, $pageSize, $userId, $stateMetadataAdapterClassName = null)
 {
     assert('is_int($pageSize)');
     assert('$stateMetadataAdapterClassName == null || is_string($stateMetadataAdapterClassName)');
     $metadataAdapter = new SearchDataProviderMetadataAdapter($searchModel, $userId, array());
     return RedBeanModelDataProviderUtil::makeDataProvider($metadataAdapter->getAdaptedMetadata(), $listModelClassName, 'RedBeanModelDataProvider', null, false, $pageSize, $stateMetadataAdapterClassName);
 }
 protected static function makeModelClassNamesAndSearchAttributeData($partialTerm, User $user, $scopeData)
 {
     assert('is_string($partialTerm)');
     assert('$user->id > 0');
     assert('$scopeData == null || is_array($scopeData)');
     $modelClassNamesAndSearchAttributeData = array();
     $modelNamesAndLabels = WorkflownQueuesSearchForm::getInQueueSearchableModelNamesAndLabels();
     foreach ($modelNamesAndLabels as $modelClassName => $notUsed) {
         $moduleClassName = $modelClassName::getModuleClassName();
         $module = Yii::app()->findModule($moduleClassName::getDirectoryName());
         $globalSearchFormClassName = $moduleClassName::getGlobalSearchFormClassName();
         if ($globalSearchFormClassName != null && RightsUtil::canUserAccessModule(get_class($module), $user) && ($scopeData == null || in_array($modelClassName, $scopeData))) {
             $searchAttributes = MixedTermSearchUtil::getGlobalSearchAttributeByModuleAndPartialTerm($module, $partialTerm);
             if (!empty($searchAttributes)) {
                 $model = new $modelClassName(false);
                 assert('$model instanceof RedBeanModel');
                 $searchForm = new $globalSearchFormClassName($model);
                 assert('$searchForm instanceof SearchForm');
                 $metadataAdapter = new SearchDataProviderMetadataAdapter($searchForm, $user->id, $searchAttributes);
                 $metadata = $metadataAdapter->getAdaptedMetadata(false);
                 $modelClassNamesAndSearchAttributeData[$globalSearchFormClassName] = array($modelClassName => $metadata);
             }
         }
     }
     return $modelClassNamesAndSearchAttributeData;
 }
 /**
  * @param   string
  * @param   bollean     Return an empty listView
  * @return  View
  */
 public function getListView($moduleName, $forceEmptyResults = false)
 {
     assert('is_string($moduleName)');
     $pageSize = $this->pageSize;
     $module = Yii::app()->findModule($moduleName);
     $searchFormClassName = $module::getGlobalSearchFormClassName();
     $modelClassName = $module::getPrimaryModelName();
     $model = new $modelClassName(false);
     $searchForm = new $searchFormClassName($model);
     $sanitizedSearchAttributes = MixedTermSearchUtil::getGlobalSearchAttributeByModuleAndPartialTerm($module, $this->term);
     $metadataAdapter = new SearchDataProviderMetadataAdapter($searchForm, $this->user->id, $sanitizedSearchAttributes);
     $listViewClassName = $module::getPluralCamelCasedName() . 'ListView';
     $sortAttribute = SearchUtil::resolveSortAttributeFromGetArray($modelClassName);
     $sortDescending = SearchUtil::resolveSortDescendingFromGetArray($modelClassName);
     if ($forceEmptyResults) {
         $dataProviderClass = 'EmptyRedBeanModelDataProvider';
         $emptyText = '';
     } else {
         $dataProviderClass = 'RedBeanModelDataProvider';
         $emptyText = null;
     }
     $dataProvider = RedBeanModelDataProviderUtil::makeDataProvider($metadataAdapter->getAdaptedMetadata(false), $modelClassName, $dataProviderClass, $sortAttribute, $sortDescending, $pageSize, $module->getStateMetadataAdapterClassName());
     $listView = new $listViewClassName('default', $module->getId(), $modelClassName, $dataProvider, GetUtil::resolveSelectedIdsFromGet(), '-' . $moduleName, array('route' => '', 'class' => 'SimpleListLinkPager', 'firstPageLabel' => '<span>first</span>', 'prevPageLabel' => '<span>previous</span>', 'nextPageLabel' => '<span>next</span>', 'lastPageLabel' => '<span>last</span>'));
     $listView->setRowsAreSelectable(false);
     $listView->setEmptyText($emptyText);
     return $listView;
 }
 public function testFullNameOnContactsSearchFormSearch()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $_FAKEPOST['Contact'] = array();
     $_FAKEPOST['Contact']['fullName'] = 'Jackie Tyler';
     $metadataAdapter = new SearchDataProviderMetadataAdapter(new ContactsSearchForm(new Contact(false)), 1, $_FAKEPOST['Contact']);
     $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
     $compareData = array('clauses' => array(1 => array('attributeName' => 'firstName', 'operatorType' => 'startsWith', 'value' => 'Jackie Tyler'), 2 => array('attributeName' => 'lastName', 'operatorType' => 'startsWith', 'value' => 'Jackie Tyler'), 3 => array('concatedAttributeNames' => array('firstName', 'lastName'), 'operatorType' => 'startsWith', 'value' => 'Jackie Tyler')), 'structure' => '(1 or 2 or 3)');
     $this->assertEquals($compareData, $searchAttributeData);
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('Contact');
     $quote = DatabaseCompatibilityUtil::getQuote();
     $where = RedBeanModelDataProvider::makeWhere('Contact', $searchAttributeData, $joinTablesAdapter);
     $compareWhere = "(({$quote}person{$quote}.{$quote}firstname{$quote} like 'Jackie Tyler%') or ";
     $compareWhere .= "({$quote}person{$quote}.{$quote}lastname{$quote} like 'Jackie Tyler%') or ";
     $compareWhere .= "(concat({$quote}person{$quote}.{$quote}firstname{$quote}, ' ', ";
     $compareWhere .= "{$quote}person{$quote}.{$quote}lastname{$quote}) like 'Jackie Tyler%'))";
     $this->assertEquals($compareWhere, $where);
     //Now test that the joinTablesAdapter has correct information.
     $this->assertEquals(1, $joinTablesAdapter->getFromTableJoinCount());
     $this->assertEquals(0, $joinTablesAdapter->getLeftTableJoinCount());
     $fromTables = $joinTablesAdapter->getFromTablesAndAliases();
     $this->assertEquals('person', $fromTables[0]['tableName']);
     //Make sure the sql runs properly.
     $dataProvider = new RedBeanModelDataProvider('Contact', null, false, $searchAttributeData);
     $data = $dataProvider->getData();
     $this->assertEquals(0, count($data));
     ContactTestHelper::createContactByNameForOwner('Dino', $super);
     $dataProvider->getTotalItemCount(true);
     //refreshes the total item count
     $data = $dataProvider->getData();
     $this->assertEquals(0, count($data));
     ContactTestHelper::createContactByNameForOwner('Jackie', $super);
     $dataProvider->getTotalItemCount(true);
     //refreshes the total item count
     $data = $dataProvider->getData();
     $this->assertEquals(0, count($data));
     ContactsModule::loadStartingData();
     $contact = new Contact();
     $contact->firstName = 'Jackie';
     $contact->lastName = 'Tyler';
     $contact->owner = $super;
     $contact->state = ContactsUtil::getStartingState();
     $this->assertTrue($contact->save());
     $dataProvider->getTotalItemCount(true);
     //refreshes the total item count
     $data = $dataProvider->getData(true);
     $this->assertEquals(1, count($data));
     $this->assertEquals($contact->id, $data[0]->id);
 }
 /**
  * Convert metadata which is just an array
  * of posted searchAttributes into metadata that is
  * readable by the RedBeanModelDataProvider
  */
 public function getAdaptedMetadata($appendStructureAsAnd = true)
 {
     $adaptedMetadata = parent::getAdaptedMetadata($appendStructureAsAnd);
     $clauseCount = count($adaptedMetadata['clauses']);
     $startingCount = $clauseCount + 1;
     $structure = '';
     if ($this->type == MissionsListConfigurationForm::LIST_TYPE_CREATED) {
         $adaptedMetadata['clauses'][$startingCount] = array('attributeName' => 'owner', 'operatorType' => 'equals', 'value' => Yii::app()->user->userModel->id);
         $structure .= $startingCount;
     } elseif ($this->type == MissionsListConfigurationForm::LIST_TYPE_MINE_TAKEN_BUT_NOT_ACCEPTED) {
         $adaptedMetadata['clauses'][$startingCount] = array('attributeName' => 'takenByUser', 'operatorType' => 'equals', 'value' => Yii::app()->user->userModel->id);
         $adaptedMetadata['clauses'][$startingCount + 1] = array('attributeName' => 'status', 'operatorType' => 'oneOf', 'value' => array(Mission::STATUS_TAKEN, Mission::STATUS_COMPLETED, Mission::STATUS_REJECTED));
         $structure .= $startingCount . ' and ' . ($startingCount + 1);
     } else {
         $adaptedMetadata['clauses'][$startingCount] = array('attributeName' => 'takenByUser', 'operatorType' => 'isNull', 'value' => null);
         $adaptedMetadata['clauses'][$startingCount + 1] = array('attributeName' => 'createdByUser', 'operatorType' => 'doesNotEqual', 'value' => Yii::app()->user->userModel->id);
         $structure .= $startingCount . ' and ' . ($startingCount + 1);
     }
     if (empty($metadata['structure'])) {
         $adaptedMetadata['structure'] = '(' . $structure . ')';
     } else {
         $adaptedMetadata['structure'] = '(' . $adaptedMetadata['structure'] . ') and (' . $structure . ')';
     }
     return $adaptedMetadata;
 }
 /**
  * Convert metadata which is just an array
  * of posted searchAttributes into metadata that is
  * readable by the RedBeanModelDataProvider
  */
 public function getAdaptedMetadata($appendStructureAsAnd = true)
 {
     $adaptedMetadata = parent::getAdaptedMetadata($appendStructureAsAnd);
     $clauseCount = count($adaptedMetadata['clauses']);
     $startingCount = $clauseCount + 1;
     $structure = '';
     if ($this->type == self::LIST_TYPE_CREATED) {
         $adaptedMetadata['clauses'][$startingCount] = array('attributeName' => 'isClosed', 'operatorType' => 'isNull', 'value' => null);
         $adaptedMetadata['clauses'][$startingCount + 1] = array('attributeName' => 'isClosed', 'operatorType' => 'equals', 'value' => 0);
         $adaptedMetadata['clauses'][$startingCount + 2] = array('attributeName' => 'owner', 'operatorType' => 'equals', 'value' => Yii::app()->user->userModel->id);
         $structure .= '( ' . $startingCount . ' or ' . ($startingCount + 1) . ' ) and ' . ($startingCount + 2);
     } elseif ($this->type == self::LIST_TYPE_PARTICIPANT) {
         $adaptedMetadata['clauses'][$startingCount] = array('attributeName' => 'isClosed', 'operatorType' => 'isNull', 'value' => null);
         $adaptedMetadata['clauses'][$startingCount + 1] = array('attributeName' => 'isClosed', 'operatorType' => 'equals', 'value' => 0);
         $adaptedMetadata['clauses'][$startingCount + 2] = array('attributeName' => 'conversationParticipants', 'relatedAttributeName' => 'person', 'operatorType' => 'equals', 'value' => Yii::app()->user->userModel->getClassId('Item'));
         $adaptedMetadata['clauses'][$startingCount + 3] = array('attributeName' => 'owner', 'operatorType' => 'equals', 'value' => Yii::app()->user->userModel->id);
         $structure .= '( ' . $startingCount . ' or ' . ($startingCount + 1) . ' ) and (' . ($startingCount + 2) . ' or ' . ($startingCount + 3) . ')';
     } elseif ($this->type == self::LIST_TYPE_CLOSED) {
         $adaptedMetadata['clauses'][$startingCount] = array('attributeName' => 'isClosed', 'operatorType' => 'equals', 'value' => true);
         $adaptedMetadata['clauses'][$startingCount + 1] = array('attributeName' => 'conversationParticipants', 'relatedAttributeName' => 'person', 'operatorType' => 'equals', 'value' => Yii::app()->user->userModel->getClassId('Item'));
         $adaptedMetadata['clauses'][$startingCount + 2] = array('attributeName' => 'owner', 'operatorType' => 'equals', 'value' => Yii::app()->user->userModel->id);
         $structure .= $startingCount . ' and (' . ($startingCount + 1) . ' or ' . ($startingCount + 2) . ')';
     }
     if (empty($metadata['structure'])) {
         $adaptedMetadata['structure'] = '(' . $structure . ')';
     } else {
         $adaptedMetadata['structure'] = '(' . $adaptedMetadata['structure'] . ') and (' . $structure . ')';
     }
     return $adaptedMetadata;
 }
Exemplo n.º 8
0
 /**
  * Utilizes information from the dataCollection object to
  * make a RedBeanDataProvider.  Either looks at saved search information or params in the $_GET array.
  * variables:
  *  modelName_sort
  *  modelName
  *  where modelName is Account for example.
  * Typically utilized by a listView action.
  */
 public function makeRedBeanDataProviderByDataCollection($searchModel, $pageSize, $stateMetadataAdapterClassName = null, $dataCollection = null)
 {
     assert('is_int($pageSize)');
     assert('$stateMetadataAdapterClassName == null || is_string($stateMetadataAdapterClassName)');
     assert('$dataCollection instanceof SearchAttributesDataCollection || $dataCollection == null');
     $listModelClassName = get_class($searchModel->getModel());
     if ($dataCollection == null) {
         $dataCollection = new SearchAttributesDataCollection($searchModel);
     }
     $searchAttributes = $dataCollection->resolveSearchAttributesFromSourceData();
     $dataCollection->resolveAnyMixedAttributesScopeForSearchModelFromSourceData();
     $dataCollection->resolveSelectedListAttributesForSearchModelFromSourceData();
     $sanitizedSearchAttributes = GetUtil::sanitizePostByDesignerTypeForSavingModel($searchModel, $searchAttributes);
     $sortAttribute = SearchUtil::resolveSortAttributeFromGetArray($listModelClassName);
     $sortDescending = SearchUtil::resolveSortDescendingFromGetArray($listModelClassName);
     $metadataAdapter = new SearchDataProviderMetadataAdapter($searchModel, Yii::app()->user->userModel->id, $sanitizedSearchAttributes);
     $metadata = static::resolveDynamicSearchMetadata($searchModel, $metadataAdapter->getAdaptedMetadata(), $dataCollection);
     return RedBeanModelDataProviderUtil::makeDataProvider($metadata, $listModelClassName, 'RedBeanModelDataProvider', $sortAttribute, $sortDescending, $pageSize, $stateMetadataAdapterClassName);
 }
Exemplo n.º 9
0
 /**
  * This test specifically looks at when searching a note's owner.  Because note extends mashableactivity which
  * does not have a bean, the query is constructed slightly different than if mashableactivity had a bean.
  */
 public function testQueryIsProperlyGeneratedForNoteWithRelatedOwnerSearch()
 {
     Yii::app()->user->userModel = User::getByUsername('super');
     $_FAKEPOST = array('Note' => array('owner' => array('id' => Yii::app()->user->userModel->id)));
     $metadataAdapter = new SearchDataProviderMetadataAdapter(new Note(false), 1, $_FAKEPOST['Note']);
     $_GET['Note_sort'] = 'description.desc';
     $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
     $quote = DatabaseCompatibilityUtil::getQuote();
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('Note');
     $where = RedBeanModelDataProvider::makeWhere('Note', $searchAttributeData, $joinTablesAdapter);
     $orderByColumnName = RedBeanModelDataProvider::resolveSortAttributeColumnName('Note', $joinTablesAdapter, 'description');
     $subsetSql = Note::makeSubsetOrCountSqlQuery('note', $joinTablesAdapter, 1, 5, $where, $orderByColumnName);
     $compareSubsetSql = "select {$quote}note{$quote}.{$quote}id{$quote} id ";
     $compareSubsetSql .= "from ({$quote}note{$quote}, {$quote}activity{$quote}, {$quote}ownedsecurableitem{$quote})";
     $compareSubsetSql .= " where ({$quote}ownedsecurableitem{$quote}.{$quote}owner__user_id{$quote} = " . Yii::app()->user->userModel->id . ")";
     $compareSubsetSql .= " and {$quote}activity{$quote}.{$quote}id{$quote} =";
     $compareSubsetSql .= " {$quote}note{$quote}.{$quote}activity_id{$quote}";
     $compareSubsetSql .= " and {$quote}ownedsecurableitem{$quote}.{$quote}id{$quote} = {$quote}activity{$quote}.{$quote}ownedsecurableitem_id{$quote}";
     $compareSubsetSql .= " order by {$quote}note{$quote}.{$quote}description{$quote} limit 5 offset 1";
     $this->assertEquals($compareSubsetSql, $subsetSql);
 }
Exemplo n.º 10
0
 /**
  * Get the content for displaying recent notifications information via an ajax call.
  * @see HeaderLinksView->renderNotificationsLinkContent()
  * @param User $user
  */
 public static function getRecentAjaxContentByUser(User $user, $count)
 {
     assert('is_int($count)');
     $content = null;
     $notification = new Notification(false);
     $searchAttributes = array('owner' => array('id' => Yii::app()->user->userModel->id));
     $metadataAdapter = new SearchDataProviderMetadataAdapter($notification, Yii::app()->user->userModel->id, $searchAttributes);
     $dataProvider = RedBeanModelDataProviderUtil::makeDataProvider($metadataAdapter->getAdaptedMetadata(), 'Notification', 'RedBeanModelDataProvider', 'createdDateTime', true, 10);
     $notifications = $dataProvider->getData();
     if (count($notifications) > 0) {
         foreach ($notifications as $notification) {
             $content .= '<div class="single-notification">';
             $content .= self::renderShortenedListViewContent($notification);
             $content .= ZurmoHtml::link("Delete<span class='icon'></span>", "#", array("class" => "remove", "onclick" => "deleteNotificationFromAjaxListView(this, " . $notification->id . ", event)"));
             $content .= '</div>';
         }
     } else {
         $content .= '<div class="single-notification">' . Zurmo::t('NotificationsModule', 'There are no recent notifications.') . '</div>';
     }
     return $content;
 }
 /**
  * Convert metadata which is just an array
  * of posted searchAttributes into metadata that is
  * readable by the RedBeanModelDataProvider
  */
 public function getAdaptedMetadata($appendStructureAsAnd = true)
 {
     $adaptedMetadata = parent::getAdaptedMetadata($appendStructureAsAnd);
     $clauseCount = count($adaptedMetadata['clauses']);
     $startingCount = $clauseCount + 1;
     $structure = '';
     $adaptedMetadata['clauses'][$startingCount] = array('attributeName' => 'id', 'operatorType' => 'equals', 'value' => $this->listId);
     $structure .= $startingCount;
     if (empty($metadata['structure'])) {
         $adaptedMetadata['structure'] = '(' . $structure . ')';
     } else {
         $adaptedMetadata['structure'] = '(' . $adaptedMetadata['structure'] . ') and (' . $structure . ')';
     }
     return $adaptedMetadata;
 }
 /**
  * Convert metadata which is just an array
  * of posted searchAttributes into metadata that is
  * readable by the RedBeanModelDataProvider
  */
 public function getAdaptedMetadata($appendStructureAsAnd = true)
 {
     $adaptedMetadata = parent::getAdaptedMetadata($appendStructureAsAnd);
     $clauseCount = count($adaptedMetadata['clauses']);
     $startingCount = $clauseCount + 1;
     $structure = '';
     $adaptedMetadata['clauses'][$startingCount] = array('attributeName' => 'folder', 'relatedAttributeName' => 'type', 'operatorType' => 'equals', 'value' => EmailFolder::TYPE_ARCHIVED_UNMATCHED);
     $adaptedMetadata['clauses'][$startingCount + 1] = array('attributeName' => 'owner', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => Yii::app()->user->userModel->id);
     $structure .= $startingCount . ' and ' . ($startingCount + 1);
     if (empty($metadata['structure'])) {
         $adaptedMetadata['structure'] = '(' . $structure . ')';
     } else {
         $adaptedMetadata['structure'] = '(' . $adaptedMetadata['structure'] . ') and (' . $structure . ')';
     }
     return $adaptedMetadata;
 }
 /**
  * @depends testSearchByOwnerId
  */
 public function testSearchByRelatedAttributeName()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $quote = DatabaseCompatibilityUtil::getQuote();
     //Test searching contacts where the related account is ABC with account id = 5.
     $fakePostData = array('account' => array('name' => 'abc'));
     $metadataAdapter = new SearchDataProviderMetadataAdapter(new Contact(false), 1, $fakePostData);
     $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('Contact');
     $where = RedBeanModelDataProvider::makeWhere('Contact', $searchAttributeData, $joinTablesAdapter);
     $compareWhere = "({$quote}account{$quote}.{$quote}name{$quote} like 'abc%')";
     $this->assertEquals($compareWhere, $where);
     //Now test that the joinTablesAdapter has correct information.
     $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
     $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
     $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
     $this->assertEquals('account', $leftTables[0]['tableName']);
     //Make sure the sql runs properly.
     $dataProvider = new RedBeanModelDataProvider('Contact', null, false, $searchAttributeData);
     $data = $dataProvider->getData();
 }
 protected static function makeModelClassNamesAndSearchAttributeData($partialTerm, User $user, $scopeData)
 {
     assert('is_string($partialTerm)');
     assert('$user->id > 0');
     assert('$scopeData == null || is_array($scopeData)');
     $modelClassNamesAndSearchAttributeData = array();
     $modules = Module::getModuleObjects();
     foreach ($modules as $module) {
         $globalSearchFormClassName = $module::getGlobalSearchFormClassName();
         if (GlobalSearchUtil::resolveIfModuleShouldBeGloballySearched($module) && $globalSearchFormClassName != null && RightsUtil::canUserAccessModule(get_class($module), $user) && ($scopeData == null || in_array($module->getName(), $scopeData))) {
             $modelClassName = $module::getPrimaryModelName();
             $searchAttributes = MixedTermSearchUtil::getGlobalSearchAttributeByModuleAndPartialTerm($module, $partialTerm);
             if (!empty($searchAttributes)) {
                 $model = new $modelClassName(false);
                 assert('$model instanceof RedBeanModel');
                 $searchForm = new $globalSearchFormClassName($model);
                 assert('$searchForm instanceof SearchForm');
                 $metadataAdapter = new SearchDataProviderMetadataAdapter($searchForm, $user->id, $searchAttributes);
                 $metadata = $metadataAdapter->getAdaptedMetadata(false);
                 $stateMetadataAdapterClassName = $module::getStateMetadataAdapterClassName();
                 if ($stateMetadataAdapterClassName != null) {
                     $stateMetadataAdapter = new $stateMetadataAdapterClassName($metadata);
                     $metadata = $stateMetadataAdapter->getAdaptedDataProviderMetadata();
                 }
                 $modelClassNamesAndSearchAttributeData[$globalSearchFormClassName] = array($modelClassName => $metadata);
             }
         }
     }
     return $modelClassNamesAndSearchAttributeData;
 }
 /**
  * @depends testSearchMemberOfAndMembers
  */
 public function testSearchByCustomFieldWithEscapedContent()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     //Searching with a custom field that is not blank should not produce any errors.
     //The data returned should be no accounts.
     $fakePostData = array('name' => null, 'officePhone' => null, 'industry' => array('value' => "Ban'king"), 'officeFax' => null);
     $account = new Account(false);
     $searchForm = new AccountsSearchForm($account);
     $metadataAdapter = new SearchDataProviderMetadataAdapter($searchForm, $super->id, $fakePostData);
     $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
     //Run search and make sure the data returned matches how many total accounts are available.
     $dataProvider = new RedBeanModelDataProvider('Account', null, false, $searchAttributeData);
     $data = $dataProvider->getData();
     $this->assertEquals(0, count($data));
 }
 public function testSearchFormDynamicAttributes()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $searchAttributes = array('aaaMember' => 'Vomitorio Corp', 'bbb' => array('relatedData' => true, 'ccc' => array('relatedData' => true, 'date__Date' => array('type' => MixedDateTypesSearchFormAttributeMappingRules::TYPE_AFTER, 'firstDate' => '1991-03-04'), 'dateTime__DateTime' => array('type' => MixedDateTypesSearchFormAttributeMappingRules::TYPE_TODAY), 'dateTime2__DateTime' => array('value' => null), 'eee' => array('relatedData' => true, 'eeeMember' => 'eeeMemberValue'), 'iii' => array('relatedData' => true, 'date__Date' => array('type' => MixedDateTypesSearchFormAttributeMappingRules::TYPE_AFTER, 'firstDate' => '1991-03-04'), 'dateTime__DateTime' => array('type' => MixedDateTypesSearchFormAttributeMappingRules::TYPE_TODAY), 'dateTime2__DateTime' => array('value' => null)))));
     $metadataAdapter = new SearchDataProviderMetadataAdapter(new AAA(false), 1, $searchAttributes);
     $todayDateTime = new DateTime(null, new DateTimeZone(Yii::app()->timeZoneHelper->getForCurrentUser()));
     $today = Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(), $todayDateTime->getTimeStamp());
     $todayPlus7Days = MixedDateTypesSearchFormAttributeMappingRules::calculateNewDateByDaysFromNow(7);
     $metadata = $metadataAdapter->getAdaptedMetadata();
     $compareClauses = array(1 => array('attributeName' => 'aaaMember', 'operatorType' => 'startsWith', 'value' => 'Vomitorio Corp'), 2 => array('attributeName' => 'bbb', 'relatedModelData' => array('attributeName' => 'ccc', 'relatedModelData' => array('attributeName' => 'date', 'operatorType' => 'greaterThanOrEqualTo', 'value' => '1991-03-04'))), 3 => array('attributeName' => 'bbb', 'relatedModelData' => array('attributeName' => 'ccc', 'relatedModelData' => array('attributeName' => 'dateTime', 'operatorType' => 'greaterThanOrEqualTo', 'value' => DateTimeUtil::convertDateIntoTimeZoneAdjustedDateTimeBeginningOfDay($today)))), 4 => array('attributeName' => 'bbb', 'relatedModelData' => array('attributeName' => 'ccc', 'relatedModelData' => array('attributeName' => 'dateTime', 'operatorType' => 'lessThanOrEqualTo', 'value' => DateTimeUtil::convertDateIntoTimeZoneAdjustedDateTimeEndOfDay($today)))), 5 => array('attributeName' => 'bbb', 'relatedModelData' => array('attributeName' => 'ccc', 'relatedModelData' => array('attributeName' => 'eee', 'relatedModelData' => array('attributeName' => 'eeeMember', 'operatorType' => 'startsWith', 'value' => 'eeeMemberValue')))), 6 => array('attributeName' => 'bbb', 'relatedModelData' => array('attributeName' => 'ccc', 'relatedModelData' => array('attributeName' => 'iii', 'relatedModelData' => array('attributeName' => 'date', 'operatorType' => 'greaterThanOrEqualTo', 'value' => '1991-03-04')))), 7 => array('attributeName' => 'bbb', 'relatedModelData' => array('attributeName' => 'ccc', 'relatedModelData' => array('attributeName' => 'iii', 'relatedModelData' => array('attributeName' => 'dateTime', 'operatorType' => 'greaterThanOrEqualTo', 'value' => DateTimeUtil::convertDateIntoTimeZoneAdjustedDateTimeBeginningOfDay($today))))), 8 => array('attributeName' => 'bbb', 'relatedModelData' => array('attributeName' => 'ccc', 'relatedModelData' => array('attributeName' => 'iii', 'relatedModelData' => array('attributeName' => 'dateTime', 'operatorType' => 'lessThanOrEqualTo', 'value' => DateTimeUtil::convertDateIntoTimeZoneAdjustedDateTimeEndOfDay($today))))));
     $compareStructure = '1 and (2) and (3 and 4) and 5 and (6) and (7 and 8)';
     $this->assertEquals($compareClauses, $metadata['clauses']);
     $this->assertEquals($compareStructure, $metadata['structure']);
 }
 /**
  * @depends testSearchByMultipleValuesCustomField
  */
 public function testSearchByTwoMultipleValuesCustomField()
 {
     if (!RedBeanDatabase::isFrozen()) {
         $quote = DatabaseCompatibilityUtil::getQuote();
         //Test where relatioon id is in a joining table.  Many to Many relationship
         $_FAKEPOST['TestOwnedCustomFieldsModel'] = array();
         $_FAKEPOST['TestOwnedCustomFieldsModel']['multipleIndustries']['values'] = array('A', 'B', 'C');
         $_FAKEPOST['TestOwnedCustomFieldsModel']['multipleSomethings']['values'] = array('D', 'E', 'F');
         $metadataAdapter = new SearchDataProviderMetadataAdapter(new TestOwnedCustomFieldsModel(false), 1, $_FAKEPOST['TestOwnedCustomFieldsModel']);
         $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
         $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('TestOwnedCustomFieldsModel');
         $where = RedBeanModelDataProvider::makeWhere('TestOwnedCustomFieldsModel', $searchAttributeData, $joinTablesAdapter);
         $compareWhere = "(1 = (select 1 from {$quote}customfieldvalue{$quote} customfieldvalue " . "where {$quote}customfieldvalue{$quote}.{$quote}multiplevaluescustomfield_id{$quote} = " . "{$quote}multiplevaluescustomfield{$quote}.id " . "and {$quote}customfieldvalue{$quote}.{$quote}value{$quote} IN('A','B','C') limit 1))";
         // Not Coding Standard
         $compareWhere .= " and (1 = (select 1 from {$quote}customfieldvalue{$quote} customfieldvalue " . "where {$quote}customfieldvalue{$quote}.{$quote}multiplevaluescustomfield_id{$quote} = " . "{$quote}multiplevaluescustomfield1{$quote}.id " . "and {$quote}customfieldvalue{$quote}.{$quote}value{$quote} IN('D','E','F') limit 1))";
         // Not Coding Standard
         $this->assertEquals($compareWhere, $where);
         //Now test that the joinTablesAdapter has correct information.
         $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
         $this->assertEquals(2, $joinTablesAdapter->getLeftTableJoinCount());
         $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
         $this->assertEquals('multiplevaluescustomfield', $leftTables[0]['tableName']);
         $this->assertEquals('multiplevaluescustomfield', $leftTables[1]['tableName']);
         //Now test that the subsetSQL query produced is correct.
         $subsetSql = TestOwnedCustomFieldsModel::makeSubsetOrCountSqlQuery('testcustomfieldsmodel', $joinTablesAdapter, 1, 5, $where, null);
         $compareSubsetSql = "select {$quote}testcustomfieldsmodel{$quote}.{$quote}id{$quote} id ";
         $compareSubsetSql .= "from {$quote}testcustomfieldsmodel{$quote} ";
         $compareSubsetSql .= "left join {$quote}multiplevaluescustomfield{$quote} on ";
         $compareSubsetSql .= "{$quote}multiplevaluescustomfield{$quote}.{$quote}id{$quote} = ";
         $compareSubsetSql .= "{$quote}testownedcustomfieldsmodel{$quote}.{$quote}multipleindustries_multiplevaluescustomfield_id{$quote} ";
         $compareSubsetSql .= "left join {$quote}multiplevaluescustomfield{$quote} multiplevaluescustomfield1 on ";
         $compareSubsetSql .= "{$quote}multiplevaluescustomfield1{$quote}.{$quote}id{$quote} = ";
         $compareSubsetSql .= "{$quote}testownedcustomfieldsmodel{$quote}.{$quote}multiplesomethings_multiplevaluescustomfield_id{$quote} ";
         $compareSubsetSql .= "where " . $compareWhere . ' ';
         $compareSubsetSql .= 'limit 5 offset 1';
         $this->assertEquals($compareSubsetSql, $subsetSql);
         //Make sure the sql runs properly.
         $dataProvider = new RedBeanModelDataProvider('TestOwnedCustomFieldsModel', null, false, $searchAttributeData);
         $data = $dataProvider->getData();
         $this->assertEquals(1, count($data));
     }
 }
 /**
  * @depends testSearchFormAnyAttributes
  */
 public function testSearchFormAnyOptOutEmail()
 {
     //get the super user here
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     //test the anyOptOut value '' for search.
     $fakePostData = array('anyOptOutEmail' => array('value' => ''));
     $metadataAdapter = new SearchDataProviderMetadataAdapter(new AccountsSearchForm(new Account(false)), $super->id, $fakePostData);
     $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
     $compareStructure = '(1 or 2)';
     $this->assertEquals(array(), $searchAttributeData['clauses']);
     $this->assertEquals(null, $searchAttributeData['structure']);
     //test the anyOptOut value '0' for search.
     $fakePostData = array('anyOptOutEmail' => array('value' => '0'));
     $metadataAdapter = new SearchDataProviderMetadataAdapter(new AccountsSearchForm(new Account(false)), $super->id, $fakePostData);
     $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
     $compareClauses = array(1 => array('attributeName' => 'primaryEmail', 'relatedAttributeName' => 'optOut', 'operatorType' => 'equals', 'value' => '0'), 2 => array('attributeName' => 'primaryEmail', 'relatedAttributeName' => 'optOut', 'operatorType' => 'isNull', 'value' => null), 3 => array('attributeName' => 'secondaryEmail', 'relatedAttributeName' => 'optOut', 'operatorType' => 'equals', 'value' => '0'), 4 => array('attributeName' => 'secondaryEmail', 'relatedAttributeName' => 'optOut', 'operatorType' => 'isNull', 'value' => null));
     $compareStructure = '((1 or 2) or (3 or 4))';
     $this->assertEquals($compareClauses, $searchAttributeData['clauses']);
     $this->assertEquals($compareStructure, $searchAttributeData['structure']);
     //test the anyOptOut value '1' for search.
     $fakePostData = array('anyOptOutEmail' => array('value' => '1'));
     $metadataAdapter = new SearchDataProviderMetadataAdapter(new AccountsSearchForm(new Account(false)), $super->id, $fakePostData);
     $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
     $compareClauses = array(1 => array('attributeName' => 'primaryEmail', 'relatedAttributeName' => 'optOut', 'operatorType' => 'equals', 'value' => (bool) 1), 2 => array('attributeName' => 'secondaryEmail', 'relatedAttributeName' => 'optOut', 'operatorType' => 'equals', 'value' => (bool) 1));
     $compareStructure = '(1 or 2)';
     $this->assertEquals($compareClauses, $searchAttributeData['clauses']);
     $this->assertEquals($compareStructure, $searchAttributeData['structure']);
 }
 protected function processData($searchAttributes, &$clauseCount, &$correctlyPositionedClauses, &$metadata)
 {
     foreach ($searchAttributes as $position => $searchAttribute) {
         $structurePosition = self::resolveUnsetAndGetSructurePosition($searchAttribute);
         self::resolveUnsetAttributeIndexOrDerivedType($searchAttribute);
         $metadataAdapter = new SearchDataProviderMetadataAdapter($this->model, $this->userId, $searchAttribute);
         $searchItemMetadata = $metadataAdapter->getAdaptedMetadata(true, $clauseCount + 1);
         if (count($searchItemMetadata['clauses']) > 0) {
             $metadata['clauses'] = $metadata['clauses'] + $searchItemMetadata['clauses'];
             $clauseCount = $clauseCount + count($searchItemMetadata['clauses']);
             $correctlyPositionedClauses[$structurePosition][$structurePosition] = $searchItemMetadata['structure'];
         } else {
             $correctlyPositionedClauses[$structurePosition][$structurePosition] = self::NOT_USED_STRUCTURE_POSITION;
         }
     }
 }
 public function testSearchFormDynamicAttributesBetweenAndOnDateTimeSearch()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $searchAttributes = array('dateTime__DateTime' => array('type' => MixedDateTypesSearchFormAttributeMappingRules::TYPE_ON, 'firstDate' => '1991-03-04'), 'dateTime2__DateTime' => array('type' => MixedDateTypesSearchFormAttributeMappingRules::TYPE_BETWEEN, 'firstDate' => '1991-03-05', 'secondDate' => '1992-04-04'));
     $searchForm = new MixedRelationsModelSearchFormTestModel(new MixedRelationsModel());
     $metadataAdapter = new SearchDataProviderMetadataAdapter($searchForm, $super->id, $searchAttributes);
     $metadata = $metadataAdapter->getAdaptedMetadata();
     $compareClauses = array(1 => array('attributeName' => 'dateTime', 'operatorType' => 'greaterThanOrEqualTo', 'value' => '1991-03-04 00:00:00'), 2 => array('attributeName' => 'dateTime', 'operatorType' => 'lessThanOrEqualTo', 'value' => '1991-03-04 23:59:59'), 3 => array('attributeName' => 'dateTime2', 'operatorType' => 'greaterThanOrEqualTo', 'value' => '1991-03-05 00:00:00'), 4 => array('attributeName' => 'dateTime2', 'operatorType' => 'lessThanOrEqualTo', 'value' => '1992-04-04 23:59:59'));
     $compareStructure = '(1 and 2) and (3 and 4)';
     $this->assertEquals($compareClauses, $metadata['clauses']);
     $this->assertEquals($compareStructure, $metadata['structure']);
 }
 /**
  * @depends testUnionSqlAcrossMultipleModelsOrderByCastedUpModelAttribute
  */
 public function testUnionSqlAcrossMultipleModelsUsingDifferentOrderBys()
 {
     $quote = DatabaseCompatibilityUtil::getQuote();
     $iFakePost = array();
     $iMetadataAdapter = new SearchDataProviderMetadataAdapter(new I(false), 1, $iFakePost);
     $jFakePost = array();
     $jMetadataAdapter = new SearchDataProviderMetadataAdapter(new J(false), 1, $jFakePost);
     $kFakePost = array();
     $kMetadataAdapter = new SearchDataProviderMetadataAdapter(new K(false), 1, $kFakePost);
     $modelClassNamesAndSearchAttributeData = array(array('I' => $iMetadataAdapter->getAdaptedMetadata()), array('J' => $jMetadataAdapter->getAdaptedMetadata()), array('K' => $kMetadataAdapter->getAdaptedMetadata()));
     $modelClassNamesAndSortAttributes = array('I' => 'name', 'J' => 'jMember', 'K' => 'kMember');
     $unionSql = RedBeanModelsDataProvider::makeUnionSql($modelClassNamesAndSearchAttributeData, $modelClassNamesAndSortAttributes, true, 2, 7);
     $compareSubsetSql = "(";
     $compareSubsetSql .= "select {$quote}i{$quote}.{$quote}id{$quote} id";
     $compareSubsetSql .= ", 'I' modelClassName, {$quote}h{$quote}.{$quote}name{$quote} orderByColumn ";
     $compareSubsetSql .= "from ({$quote}i{$quote}, {$quote}h{$quote}) ";
     $compareSubsetSql .= " where {$quote}h{$quote}.{$quote}id{$quote} = {$quote}i{$quote}.{$quote}h_id{$quote}";
     $compareSubsetSql .= ") ";
     $compareSubsetSql .= "UNION (";
     $compareSubsetSql .= "select {$quote}j{$quote}.{$quote}id{$quote} id";
     $compareSubsetSql .= ", 'J' modelClassName, {$quote}j{$quote}.{$quote}jmember{$quote} orderByColumn ";
     $compareSubsetSql .= "from {$quote}j{$quote} ";
     $compareSubsetSql .= ") ";
     $compareSubsetSql .= "UNION (";
     $compareSubsetSql .= "select {$quote}k{$quote}.{$quote}id{$quote} id";
     $compareSubsetSql .= ", 'K' modelClassName, {$quote}k{$quote}.{$quote}kmember{$quote} orderByColumn ";
     $compareSubsetSql .= "from {$quote}k{$quote} ";
     $compareSubsetSql .= ") ";
     $compareSubsetSql .= "order by orderByColumn desc ";
     $compareSubsetSql .= 'limit 7 offset 2';
     $this->assertEquals($compareSubsetSql, $unionSql);
     //Make sure the sql runs properly.
     $dataProvider = new RedBeanModelsDataProvider('anId', $modelClassNamesAndSortAttributes, true, $modelClassNamesAndSearchAttributeData);
     $data = $dataProvider->getData();
     //Test results are correct
     $this->assertEquals(9, count($data));
 }
 /**
  * @depends testSetAndGetMultiSelectDropDownAttribute
  */
 public function testSearchForMultiSelectDropDownAttributePlacedForAccountsModule()
 {
     //Test that the multiple select attribute can query properly for search.
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     //Create an account to test searching multiple fields on for search.
     $account = new Account();
     $this->assertEquals(1, $account->testHobbiesCstm->values->count());
     $account->name = 'my test account';
     $account->owner = Yii::app()->user->userModel;
     $customFieldValue2 = new CustomFieldValue();
     $customFieldValue2->value = 'Reading';
     $account->testHobbiesCstm->values->add($customFieldValue2);
     $this->assertTrue($account->save());
     $accountId = $account->id;
     $account = Account::getById($accountId);
     $this->assertEquals(2, $account->testHobbiesCstm->values->count());
     $this->assertContains('Writing', $account->testHobbiesCstm->values);
     $this->assertContains('Reading', $account->testHobbiesCstm->values);
     //Create a second account with different hobbies
     $account = new Account();
     //Remove the default value of 'Writing';
     $account->testHobbiesCstm->values->removeByIndex(0);
     $account->name = 'my test account2';
     $account->owner = Yii::app()->user->userModel;
     $customFieldValue1 = new CustomFieldValue();
     $customFieldValue1->value = 'Singing';
     $account->testHobbiesCstm->values->add($customFieldValue1);
     $customFieldValue2 = new CustomFieldValue();
     $customFieldValue2->value = 'Surfing';
     $account->testHobbiesCstm->values->add($customFieldValue2);
     $this->assertTrue($account->save());
     $accountId = $account->id;
     $account = Account::getById($accountId);
     $this->assertEquals(2, $account->testHobbiesCstm->values->count());
     $this->assertContains('Singing', $account->testHobbiesCstm->values);
     $this->assertContains('Surfing', $account->testHobbiesCstm->values);
     //Searching with a custom field that is not blank should not produce an errors.
     $searchPostData = array('name' => 'my test account', 'officePhone' => '', 'testHobbies' => array('values' => array(0 => '')), 'officeFax' => '');
     $modifiedSearchPostData = SearchUtil::getSearchAttributesFromSearchArray($searchPostData);
     $this->assertEquals(array('name' => 'my test account', 'officePhone' => null, 'officeFax' => null), $modifiedSearchPostData);
     $account = new Account(false);
     $searchForm = new AccountsSearchForm($account);
     $metadataAdapter = new SearchDataProviderMetadataAdapter($searchForm, $super->id, $modifiedSearchPostData);
     $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
     //Make sure the search params are empty
     $compareClauses = array(1 => array('attributeName' => 'name', 'operatorType' => 'startsWith', 'value' => 'my test account'));
     $compareStructure = '1';
     $this->assertEquals($compareClauses, $searchAttributeData['clauses']);
     $this->assertEquals($compareStructure, $searchAttributeData['structure']);
     //Run search and make sure the data returned matches how many total accounts are available.
     $dataProvider = new RedBeanModelDataProvider('Account', null, false, $searchAttributeData);
     $data = $dataProvider->getData();
     $this->assertEquals(2, count($data));
 }
 public function testUnionQuery()
 {
     //regular user no elevation
     $user = new User();
     $user->username = '******';
     $user->firstName = 'aaa';
     $user->lastName = 'aaa';
     $saved = $user->save();
     $this->assertTrue($saved);
     $aUser = User::getByUsername('aaa');
     Yii::app()->user->userModel = $aUser;
     $mungeIds = AllPermissionsOptimizationUtil::getMungeIdsByUser($aUser);
     $model = new OwnedSecurableTestItem();
     $model->member = 'test4';
     $model->owner = $aUser;
     $this->assertTrue($model->save());
     Yii::app()->user->userModel = User::getByUsername('super');
     $this->assertEquals(4, OwnedSecurableTestItem::getCount());
     $model = new OwnedSecurableTestItem2();
     $model->member = 'test4';
     $model->owner = $aUser;
     $this->assertTrue($model->save());
     $this->assertEquals(2, OwnedSecurableTestItem2::getCount());
     Yii::app()->user->userModel = User::getByUsername('aaa');
     $quote = DatabaseCompatibilityUtil::getQuote();
     //Test search attribute data across multiple models.
     $aFakePost = array('member' => 'test4');
     $aMetadataAdapter = new SearchDataProviderMetadataAdapter(new OwnedSecurableTestItem(false), $aUser->id, $aFakePost);
     $bFakePost = array('member' => 'test4');
     $bMetadataAdapter = new SearchDataProviderMetadataAdapter(new OwnedSecurableTestItem2(false), $aUser->id, $bFakePost);
     $modelClassNamesAndSearchAttributeData = array(array('OwnedSecurableTestItem' => $aMetadataAdapter->getAdaptedMetadata()), array('OwnedSecurableTestItem2' => $bMetadataAdapter->getAdaptedMetadata()));
     $unionSql = RedBeanModelsDataProvider::makeUnionSql($modelClassNamesAndSearchAttributeData, null, false, 2, 7);
     $compareWhere = "({$quote}ownedsecurabletestitem{$quote}.{$quote}member{$quote} like 'test4%')";
     $compareWhere2 = "({$quote}ownedsecurabletestitem2{$quote}.{$quote}member{$quote} like 'test4%')";
     $compareSubsetSql = "(";
     $compareSubsetSql .= "select distinct {$quote}ownedsecurabletestitem{$quote}.{$quote}id{$quote} id";
     $compareSubsetSql .= ", 'OwnedSecurableTestItem' modelClassName ";
     $compareSubsetSql .= "from ({$quote}ownedsecurabletestitem{$quote}, {$quote}ownedsecurableitem{$quote}) ";
     $compareSubsetSql .= "left join {$quote}ownedsecurabletestitem_read{$quote} on ";
     $compareSubsetSql .= "{$quote}ownedsecurabletestitem_read{$quote}.{$quote}securableitem_id{$quote} = ";
     $compareSubsetSql .= "{$quote}ownedsecurableitem{$quote}.{$quote}securableitem_id{$quote} ";
     $compareSubsetSql .= "and {$quote}munge_id{$quote} in ('" . join("', '", $mungeIds) . "') ";
     $compareSubsetSql .= "where ({$compareWhere}) and ({$quote}ownedsecurableitem{$quote}.{$quote}owner__user_id{$quote} = {$aUser->id} ";
     $compareSubsetSql .= "OR {$quote}ownedsecurabletestitem_read{$quote}.{$quote}munge_id{$quote} IS NOT NULL) ";
     // Not Coding Standard
     $compareSubsetSql .= "and {$quote}ownedsecurableitem{$quote}.{$quote}id{$quote} = ";
     $compareSubsetSql .= "{$quote}ownedsecurabletestitem{$quote}.{$quote}ownedsecurableitem_id{$quote}";
     $compareSubsetSql .= ") ";
     $compareSubsetSql .= "UNION (";
     $compareSubsetSql .= "select distinct {$quote}ownedsecurabletestitem2{$quote}.{$quote}id{$quote} id";
     $compareSubsetSql .= ", 'OwnedSecurableTestItem2' modelClassName ";
     $compareSubsetSql .= "from ({$quote}ownedsecurabletestitem2{$quote}, {$quote}ownedsecurableitem{$quote}) ";
     $compareSubsetSql .= "left join {$quote}ownedsecurabletestitem2_read{$quote} on ";
     $compareSubsetSql .= "{$quote}ownedsecurabletestitem2_read{$quote}.{$quote}securableitem_id{$quote} = ";
     $compareSubsetSql .= "{$quote}ownedsecurableitem{$quote}.{$quote}securableitem_id{$quote} ";
     $compareSubsetSql .= "and {$quote}munge_id{$quote} in ('" . join("', '", $mungeIds) . "') ";
     $compareSubsetSql .= "where ({$compareWhere2}) and ({$quote}ownedsecurableitem{$quote}.{$quote}owner__user_id{$quote} = {$aUser->id} ";
     $compareSubsetSql .= "OR {$quote}ownedsecurabletestitem2_read{$quote}.{$quote}munge_id{$quote} IS NOT NULL) ";
     // Not Coding Standard
     $compareSubsetSql .= "and {$quote}ownedsecurableitem{$quote}.{$quote}id{$quote} = ";
     $compareSubsetSql .= "{$quote}ownedsecurabletestitem2{$quote}.{$quote}ownedsecurableitem_id{$quote}";
     $compareSubsetSql .= ") ";
     $compareSubsetSql .= 'limit 7 offset 2';
     $this->assertEquals($compareSubsetSql, $unionSql);
     //Make sure the sql runs properly.
     $dataProvider = new RedBeanModelsDataProvider('anId', null, false, $modelClassNamesAndSearchAttributeData);
     $data = $dataProvider->getData();
     //Test results are correct
     $this->assertEquals(2, count($data));
     $this->assertEquals('OwnedSecurableTestItem', get_class($data[0]));
     $this->assertEquals('OwnedSecurableTestItem2', get_class($data[1]));
     //Make sure union count query produces the same count.
     $this->assertEquals(2, $dataProvider->calculateTotalItemCount());
 }
 /**
  * @depends testManyManyRelationSqlQuery
  * See ModelDataProviderUtilTest for more order by testing.
  */
 public function testOrderByCombinations()
 {
     $gg = new GG();
     $gg->gg = 'a';
     $gg->g = 'v';
     $this->assertTrue($gg->save());
     $gg = new GG();
     $gg->gg = 'b';
     $gg->g = 't';
     $this->assertTrue($gg->save());
     $gg = new GG();
     $gg->gg = 'c';
     $gg->g = 'u';
     $this->assertTrue($gg->save());
     $quote = DatabaseCompatibilityUtil::getQuote();
     $_FAKEPOST['GG'] = array();
     $metadataAdapter = new SearchDataProviderMetadataAdapter(new GG(false), 1, $_FAKEPOST['GG']);
     $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
     $dataProvider = new RedBeanModelDataProvider('GG', 'gg', false, $searchAttributeData);
     $data = $dataProvider->getData();
     $this->assertEquals(3, count($data));
     $this->assertEquals('a', $data[0]->gg);
     $this->assertEquals('b', $data[1]->gg);
     $this->assertEquals('c', $data[2]->gg);
     $dataProvider = new RedBeanModelDataProvider('GG', 'gg', true, $searchAttributeData);
     $data = $dataProvider->getData();
     $this->assertEquals(3, count($data));
     $this->assertEquals('c', $data[0]->gg);
     $this->assertEquals('b', $data[1]->gg);
     $this->assertEquals('a', $data[2]->gg);
     $compareString = "{$quote}gg{$quote}.{$quote}gg{$quote}";
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('GG');
     $this->assertEquals($compareString, RedBeanModelDataProvider::resolveSortAttributeColumnName('GG', $joinTablesAdapter, 'gg'));
     $compareString = "{$quote}g{$quote}.{$quote}g{$quote}";
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('GG');
     $this->assertEquals($compareString, RedBeanModelDataProvider::resolveSortAttributeColumnName('GG', $joinTablesAdapter, 'g'));
     //test ordering by castedUp model.
     $dataProvider = new RedBeanModelDataProvider('GG', 'g', false, $searchAttributeData);
     $data = $dataProvider->getData();
     $this->assertEquals(3, count($data));
     $this->assertEquals('b', $data[0]->gg);
     $this->assertEquals('c', $data[1]->gg);
     $this->assertEquals('a', $data[2]->gg);
     $dataProvider = new RedBeanModelDataProvider('GG', 'g', true, $searchAttributeData);
     $data = $dataProvider->getData();
     $this->assertEquals(3, count($data));
     $this->assertEquals('a', $data[0]->gg);
     $this->assertEquals('c', $data[1]->gg);
     $this->assertEquals('b', $data[2]->gg);
     //test ordering by custom attribute value.
 }
Exemplo n.º 25
0
 /**
  * Given a model and an attribute that is a relation, ascertain the correct model to use.  If a search form
  * model is available then use that otherwise use the appropriate related model.
  * @param object $model SearchForm or RedBeanModel
  * @param string $attributeName
  */
 public static function resolveModelToUseByModelAndAttributeName($model, $attributeName)
 {
     assert('$model instanceof SearchForm || $model instanceof RedBeanModel');
     assert('is_string($attributeName)');
     $modelToUse = SearchDataProviderMetadataAdapter::resolveAsRedBeanModel($model->{$attributeName});
     $moduleClassName = $modelToUse->getModuleClassName();
     if ($moduleClassName != null) {
         $formClassName = $moduleClassName::getGlobalSearchFormClassName();
         if ($formClassName != null) {
             $modelToUse = new $formClassName($modelToUse);
         }
     }
     return $modelToUse;
 }
 /**
  * @depends testSearchCrossingTables
  * @depends testGetAllModels
  */
 public function testSearchUserFirstName()
 {
     $_FAKEPOST = array('User' => array('firstName' => 'billy'));
     $metadataAdapter = new SearchDataProviderMetadataAdapter(new User(false), 1, $_FAKEPOST['User']);
     $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
     $dataProvider = new RedBeanModelDataProvider('User', null, false, $searchAttributeData);
     $users = $dataProvider->getData();
     $this->assertEquals(1, count($users));
     $this->assertEquals(1, $dataProvider->calculateTotalItemCount());
 }