public static function getContactIdsByReportDataProviderAndAttributeName(RowsAndColumnsReportDataProvider $reportDataProvider, $attributeName)
 {
     $contactIds = array();
     $reportResultsRowDataItems = $reportDataProvider->getData();
     foreach ($reportResultsRowDataItems as $reportResultsRowDataItem) {
         $contact = $reportResultsRowDataItem->getModel($attributeName);
         $contactIds[] = $contact->id;
     }
     return $contactIds;
 }
 public function testSqlQueryWithLinkTypeSpecificOnRelatedModels()
 {
     $quote = DatabaseCompatibilityUtil::getQuote();
     Yii::app()->user->userModel = User::getByUsername('super');
     $reportModelTestItem2 = new ReportModelTestItem2();
     $reportModelTestItem2->name = 'name';
     $this->assertTrue($reportModelTestItem2->save());
     $reportModelTestItem = new ReportModelTestItem();
     $reportModelTestItem->lastName = 'lastName';
     $reportModelTestItem->string = 'string';
     $reportModelTestItem->hasOne = $reportModelTestItem2;
     $this->assertTrue($reportModelTestItem->save());
     $report = new Report();
     $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
     $report->setModuleClassName('ReportsTest2Module');
     $report->setFiltersStructure('');
     $displayAttribute1 = new DisplayAttributeForReportForm('ReportsTest2Module', 'ReportModelTestItem2', Report::TYPE_ROWS_AND_COLUMNS);
     $displayAttribute1->setModelAliasUsingTableAliasName('relatedModel');
     $displayAttribute1->attributeIndexOrDerivedType = 'hasMany2___FullName';
     $report->addDisplayAttribute($displayAttribute1);
     $dataProvider = new RowsAndColumnsReportDataProvider($report);
     $content = $dataProvider->makeSqlQueryForDisplay();
     $compareContent = "select {$quote}reportmodeltestitem{$quote}.{$quote}id{$quote} reportmodeltestitemid " . "from {$quote}reportmodeltestitem2{$quote} " . "left join {$quote}reportmodeltestitem{$quote} on " . "{$quote}reportmodeltestitem{$quote}.{$quote}hasone_reportmodeltestitem2_id{$quote} " . "= {$quote}reportmodeltestitem2{$quote}.{$quote}id{$quote}  limit 10 offset 0";
     $this->assertEquals($compareContent, $content);
 }
 /**
  * Running a report centered on notes, with a display attribute from notes and accounts. With a created
  * by dateTime filter should produce proper query filter notes.
  * This test just makes sure the sql is structured properly
  */
 public function testFiltersWorksOnAccountsAndNoteReport()
 {
     $report = new Report();
     $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
     $filter = new FilterForReportForm('NotesModule', 'Note', $report->getType());
     $filter->attributeIndexOrDerivedType = 'createdDateTime';
     $filter->valueType = MixedDateTypesSearchFormAttributeMappingRules::TYPE_BETWEEN;
     $filter->value = '1991-05-05';
     $filter->secondValue = '1991-06-05';
     $filter->availableAtRunTime = true;
     $report->addFilter($filter);
     $report->setFiltersStructure('1');
     $report->setModuleClassName('NotesModule');
     $displayAttribute = new DisplayAttributeForReportForm('NotesModule', 'Note', $report->getType());
     $displayAttribute->attributeIndexOrDerivedType = 'description';
     $report->addDisplayAttribute($displayAttribute);
     $displayAttribute2 = new DisplayAttributeForReportForm('NotesModule', 'Note', $report->getType());
     $displayAttribute2->attributeIndexOrDerivedType = 'Account__activityItems__Inferred___name';
     $report->addDisplayAttribute($displayAttribute2);
     $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
     $sql = $reportDataProvider->makeSqlQueryForDisplay();
     $q = DatabaseCompatibilityUtil::getQuote();
     $compareSql = "select {$q}note{$q}.{$q}id{$q} noteid, {$q}account{$q}.{$q}id{$q} accountid " . "from ({$q}note{$q}, {$q}activity{$q}, {$q}ownedsecurableitem{$q} ownedsecurableitem1, {$q}securableitem{$q} securableitem1, {$q}item{$q} item1) " . "left join {$q}activity_item{$q} on {$q}activity_item{$q}.{$q}activity_id{$q} = {$q}activity{$q}.{$q}id{$q} " . "left join {$q}item{$q} on {$q}item{$q}.{$q}id{$q} = {$q}activity_item{$q}.{$q}item_id{$q} " . "left join {$q}securableitem{$q} on {$q}securableitem{$q}.{$q}item_id{$q} = {$q}item{$q}.{$q}id{$q} " . "left join {$q}ownedsecurableitem{$q} on {$q}ownedsecurableitem{$q}.{$q}securableitem_id{$q} = {$q}securableitem{$q}.{$q}id{$q} " . "left join {$q}account{$q} on {$q}account{$q}.{$q}ownedsecurableitem_id{$q} = {$q}ownedsecurableitem{$q}.{$q}id{$q} " . "where ((({$q}item1{$q}.{$q}createddatetime{$q} >= '1991-05-05 00:00:00') " . "and ({$q}item1{$q}.{$q}createddatetime{$q} <= '1991-06-05 23:59:59'))) " . "and {$q}activity{$q}.{$q}id{$q} = {$q}note{$q}.{$q}activity_id{$q} " . "and {$q}ownedsecurableitem1{$q}.{$q}id{$q} = {$q}activity{$q}.{$q}ownedsecurableitem_id{$q} " . "and {$q}securableitem1{$q}.{$q}id{$q} = {$q}ownedsecurableitem1{$q}.{$q}securableitem_id{$q} " . "and {$q}item1{$q}.{$q}id{$q} = {$q}securableitem1{$q}.{$q}item_id{$q} limit 10 offset 0";
     $this->assertEquals($compareSql, $sql);
 }
 public function testResolveFiltersForVariableStatesWithOneDisplayAttributeAndOneFilterForANonSuperUserWhoCanSeeAnotherState()
 {
     Yii::app()->user->userModel = User::getByUsername('sarah');
     $report = new Report();
     $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
     $report->setModuleClassName('AccountsModule');
     $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', Report::TYPE_ROWS_AND_COLUMNS);
     $displayAttribute->attributeIndexOrDerivedType = 'contacts___officePhone';
     $report->addDisplayAttribute($displayAttribute);
     $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
     $filtersStructure = '1';
     $filter = new FilterForReportForm('AccountsModule', 'Account', Report::TYPE_ROWS_AND_COLUMNS);
     $filter->attributeIndexOrDerivedType = 'opportunities___contacts___website';
     $filter->operator = OperatorRules::TYPE_EQUALS;
     $filter->value = 'Zurmo';
     $filters = array($filter);
     $filters = $reportDataProvider->resolveFiltersForVariableStates($filters, $filtersStructure);
     $stateAdapter = new LeadsStateMetadataAdapter(array('clauses' => array(), 'structure' => ''));
     $this->assertTrue(count($stateAdapter->getStateIds()) > 0);
     $this->assertEquals(3, count($filters));
     $this->assertEquals('contacts___state', $filters[1]->attributeIndexOrDerivedType);
     $this->assertEquals(OperatorRules::TYPE_ONE_OF, $filters[1]->operator);
     $this->assertEquals($stateAdapter->getStateIds(), $filters[1]->value);
     $this->assertEquals('opportunities___contacts___state', $filters[2]->attributeIndexOrDerivedType);
     $this->assertEquals(OperatorRules::TYPE_ONE_OF, $filters[2]->operator);
     $this->assertEquals($stateAdapter->getStateIds(), $filters[2]->value);
     $this->assertEquals('1 and (2 and 3)', $filtersStructure);
 }
 /**
  * Running a report centered on notes, with a display attribute from notes and accounts.  Ordered by
  * created date time in notes.  Should produce proper query to order by notes.  This test just makes sure
  * the sql is structured properly
  */
 public function testOrderByWorksOnAccountsAndNoteReport()
 {
     $report = new Report();
     $report->setFiltersStructure('');
     $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
     $report->setModuleClassName('NotesModule');
     $displayAttribute = new DisplayAttributeForReportForm('NotesModule', 'Note', $report->getType());
     $displayAttribute->attributeIndexOrDerivedType = 'description';
     $report->addDisplayAttribute($displayAttribute);
     $displayAttribute2 = new DisplayAttributeForReportForm('NotesModule', 'Note', $report->getType());
     $displayAttribute2->attributeIndexOrDerivedType = 'Account__activityItems__Inferred___name';
     $report->addDisplayAttribute($displayAttribute2);
     $orderBy = new OrderByForReportForm('NotesModule', 'Note', Report::TYPE_SUMMATION);
     $orderBy->attributeIndexOrDerivedType = 'createdDateTime';
     $report->addOrderBy($orderBy);
     $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
     $sql = $reportDataProvider->makeSqlQueryForDisplay();
     $q = DatabaseCompatibilityUtil::getQuote();
     $compareSql = "select {$q}note{$q}.{$q}id{$q} noteid, {$q}account{$q}.{$q}id{$q} accountid " . "from ({$q}note{$q}, {$q}activity{$q}, {$q}ownedsecurableitem{$q} ownedsecurableitem1, {$q}securableitem{$q} securableitem1, {$q}item{$q} item1) " . "left join {$q}activity_item{$q} on {$q}activity_item{$q}.{$q}activity_id{$q} = {$q}activity{$q}.{$q}id{$q} " . "left join {$q}item{$q} on {$q}item{$q}.{$q}id{$q} = {$q}activity_item{$q}.{$q}item_id{$q} " . "left join {$q}securableitem{$q} on {$q}securableitem{$q}.{$q}item_id{$q} = {$q}item{$q}.{$q}id{$q} " . "left join {$q}ownedsecurableitem{$q} on {$q}ownedsecurableitem{$q}.{$q}securableitem_id{$q} = {$q}securableitem{$q}.{$q}id{$q} " . "left join {$q}account{$q} on {$q}account{$q}.{$q}ownedsecurableitem_id{$q} = {$q}ownedsecurableitem{$q}.{$q}id{$q}  " . "where {$q}activity{$q}.{$q}id{$q} = {$q}note{$q}.{$q}activity_id{$q} and " . "{$q}ownedsecurableitem1{$q}.{$q}id{$q} = {$q}activity{$q}.{$q}ownedsecurableitem_id{$q} " . "and {$q}securableitem1{$q}.{$q}id{$q} = {$q}ownedsecurableitem1{$q}.{$q}securableitem_id{$q} and " . "{$q}item1{$q}.{$q}id{$q} = {$q}securableitem1{$q}.{$q}item_id{$q} order by {$q}item1{$q}.{$q}createddatetime{$q} asc limit 10 offset 0";
     $this->assertEquals($compareSql, $sql);
 }