public function testResolveSearchAttributesDataByRelatedItemIdWithRegularUser()
 {
     Yii::app()->user->userModel = User::getByUsername('benny');
     $mungeIds = ReadPermissionsOptimizationUtil::getMungeIdsByUser(Yii::app()->user->userModel);
     $quote = DatabaseCompatibilityUtil::getQuote();
     $rules = new EmailMessageMashableActivityRules();
     $searchAttributeData = $rules->resolveSearchAttributesDataByRelatedItemId(5);
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('EmailMessage');
     $where = RedBeanModelDataProvider::makeWhere('EmailMessage', $searchAttributeData, $joinTablesAdapter);
     $compareWhere = "(({$quote}emailmessagesender{$quote}.{$quote}personoraccount_item_id{$quote} = 5) or (1 = ";
     $compareWhere .= "(select 1 from {$quote}emailmessagerecipient{$quote} emailmessagerecipient where ";
     $compareWhere .= "{$quote}emailmessagerecipient{$quote}.{$quote}emailmessage_id` = {$quote}emailmessage";
     $compareWhere .= "{$quote}.id and {$quote}emailmessagerecipient{$quote}.{$quote}personoraccount_item_id` = 5 limit 1)))";
     $this->assertEquals($compareWhere, $where);
     $sql = EmailMessage::makeSubsetOrCountSqlQuery('emailmessage', $joinTablesAdapter, 1, 5, $where, null);
     $compareSubsetSql = "select distinct {$quote}emailmessage{$quote}.{$quote}id{$quote} id ";
     $compareSubsetSql .= "from ({$quote}emailmessage{$quote}, {$quote}ownedsecurableitem{$quote}) ";
     $compareSubsetSql .= "left join {$quote}emailmessagesender{$quote} on ";
     $compareSubsetSql .= "{$quote}emailmessagesender{$quote}.{$quote}id{$quote} = {$quote}emailmessage{$quote}.";
     $compareSubsetSql .= "{$quote}sender_emailmessagesender_id{$quote} ";
     $compareSubsetSql .= "left join {$quote}emailmessage_read{$quote} on ";
     $compareSubsetSql .= "{$quote}emailmessage_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 . ') ';
     $compareSubsetSql .= "and ({$quote}ownedsecurableitem{$quote}.{$quote}owner__user_id{$quote} = " . Yii::app()->user->userModel->id . " ";
     // Not Coding Standard
     $compareSubsetSql .= "OR {$quote}emailmessage_read{$quote}.{$quote}munge_id{$quote} IS NOT NULL) ";
     // Not Coding Standard
     $compareSubsetSql .= "and {$quote}ownedsecurableitem{$quote}.{$quote}id{$quote} = ";
     $compareSubsetSql .= "{$quote}emailmessage{$quote}.{$quote}ownedsecurableitem_id{$quote} ";
     $compareSubsetSql .= 'limit 5 offset 1';
     $this->assertEquals($compareSubsetSql, $sql);
     //Make sure the sql runs properly.
     $data = EmailMessage::getSubset($joinTablesAdapter, 0, 5, $where, null);
     $this->assertEquals(0, count($data));
 }