public function testCanCurrentUserProperlyRenderResults()
 {
     $billy = User::getByUsername('billy');
     $billy->setRight('AccountsModule', AccountsModule::RIGHT_ACCESS_ACCOUNTS);
     $billy->setRight('OpportunitiesModule', OpportunitiesModule::RIGHT_ACCESS_OPPORTUNITIES);
     $saved = $billy->save();
     $this->assertTrue($saved);
     $report = new Report();
     $report->setModuleClassName('ContactsModule');
     $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
     $this->assertTrue($report->canCurrentUserProperlyRenderResults());
     //Now set to Billy
     Yii::app()->user->userModel = $billy;
     //Billy can't see the contacts module.
     $this->assertFalse($report->canCurrentUserProperlyRenderResults());
     //Billy can see accounts
     $report->setModuleClassName('AccountsModule');
     $this->assertTrue($report->canCurrentUserProperlyRenderResults());
     //A filter on accounts is ok for Billy to see
     $filter = new FilterForReportForm('AccountsModule', 'Account', $report->getType());
     $filter->attributeIndexOrDerivedType = 'officePhone';
     $filter->value = 'aValue';
     $filter->operator = 'equals';
     $report->addFilter($filter);
     $this->assertTrue($report->canCurrentUserProperlyRenderResults());
     //A filter on contacts is not ok for Billy to see
     $filter2 = new FilterForReportForm('AccountsModule', 'Account', $report->getType());
     $filter2->attributeIndexOrDerivedType = 'contacts___lastName';
     $filter2->value = 'aValue';
     $filter2->operator = 'equals';
     $report->addFilter($filter2);
     $this->assertFalse($report->canCurrentUserProperlyRenderResults());
     //A related filter on opportunities would be ok for Billy to see
     $report->removeAllFilters();
     $filter = new FilterForReportForm('AccountsModule', 'Account', $report->getType());
     $filter->attributeIndexOrDerivedType = 'opportunities___name';
     $filter->value = 'aValue';
     $filter->operator = 'equals';
     $report->addFilter($filter);
     $this->assertTrue($report->canCurrentUserProperlyRenderResults());
     $report->removeAllFilters();
     //Billy can see a groupBy on Accounts
     $groupBy = new GroupByForReportForm('AccountsModule', 'Account', $report->getType());
     $groupBy->attributeIndexOrDerivedType = 'name';
     $groupBy->axis = 'y';
     $report->addGroupBy($groupBy);
     $this->assertTrue($report->canCurrentUserProperlyRenderResults());
     //Billy cannot see a related groupBy on Contacts
     $groupBy = new GroupByForReportForm('AccountsModule', 'Account', $report->getType());
     $groupBy->attributeIndexOrDerivedType = 'contacts___lastName';
     $groupBy->axis = 'y';
     $report->addGroupBy($groupBy);
     $this->assertFalse($report->canCurrentUserProperlyRenderResults());
     //Billy can see a related groupBy on Opportunities
     $report->removeAllGroupBys();
     $groupBy = new GroupByForReportForm('AccountsModule', 'Account', $report->getType());
     $groupBy->attributeIndexOrDerivedType = 'opportunities___name';
     $groupBy->axis = 'y';
     $report->addGroupBy($groupBy);
     $this->assertTrue($report->canCurrentUserProperlyRenderResults());
     $report->removeAllGroupBys();
     //Billy can see an orderBy on Accounts
     $orderBy = new OrderByForReportForm('AccountsModule', 'Account', $report->getType());
     $orderBy->attributeIndexOrDerivedType = 'name';
     $orderBy->order = 'desc';
     $report->addOrderBy($orderBy);
     $this->assertTrue($report->canCurrentUserProperlyRenderResults());
     //Billy cannot see a related orderBy on Contacts
     $orderBy = new OrderByForReportForm('AccountsModule', 'Account', $report->getType());
     $orderBy->attributeIndexOrDerivedType = 'contacts___lastName';
     $orderBy->order = 'desc';
     $report->addOrderBy($orderBy);
     $this->assertFalse($report->canCurrentUserProperlyRenderResults());
     //Billy can see a related orderBy on Opportunities
     $report->removeAllOrderBys();
     $orderBy = new OrderByForReportForm('AccountsModule', 'Account', $report->getType());
     $orderBy->attributeIndexOrDerivedType = 'opportunities___name';
     $orderBy->order = 'desc';
     $report->addOrderBy($orderBy);
     $this->assertTrue($report->canCurrentUserProperlyRenderResults());
     $report->removeAllOrderBys();
     //Billy can see a displayAttribute on Accounts
     $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
     $displayAttribute->attributeIndexOrDerivedType = 'name';
     $displayAttribute->label = 'someNewLabel';
     $report->addDisplayAttribute($displayAttribute);
     $this->assertTrue($report->canCurrentUserProperlyRenderResults());
     //Billy cannot see a related displayAttribute on Contacts
     $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
     $displayAttribute->attributeIndexOrDerivedType = 'contacts___firstName';
     $displayAttribute->label = 'someNewLabel';
     $report->addDisplayAttribute($displayAttribute);
     $this->assertFalse($report->canCurrentUserProperlyRenderResults());
     //Billy can see a related displayAttribute on Opportunities
     $report->removeAllDisplayAttributes();
     $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
     $displayAttribute->attributeIndexOrDerivedType = 'opportunities___name';
     $displayAttribute->label = 'someNewLabel';
     $report->addDisplayAttribute($displayAttribute);
     $this->assertTrue($report->canCurrentUserProperlyRenderResults());
     $report->removeAllDisplayAttributes();
     //Billy can see a drillDownDisplayAttribute on Accounts
     $drillDownDisplayAttribute = new DrillDownDisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
     $drillDownDisplayAttribute->attributeIndexOrDerivedType = 'name';
     $drillDownDisplayAttribute->label = 'someNewLabel';
     $report->addDrillDownDisplayAttribute($drillDownDisplayAttribute);
     $this->assertTrue($report->canCurrentUserProperlyRenderResults());
     //Billy cannot see a related drillDownDisplayAttribute on Contacts
     $drillDownDisplayAttribute = new DrillDownDisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
     $drillDownDisplayAttribute->attributeIndexOrDerivedType = 'contacts___firstName';
     $drillDownDisplayAttribute->label = 'someNewLabel';
     $report->addDrillDownDisplayAttribute($drillDownDisplayAttribute);
     $this->assertFalse($report->canCurrentUserProperlyRenderResults());
     //Billy can see a related drillDownDisplayAttribute on Opportunities
     $report->removeAllDrillDownDisplayAttributes();
     $drillDownDisplayAttribute = new DrillDownDisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
     $drillDownDisplayAttribute->attributeIndexOrDerivedType = 'opportunities___name';
     $drillDownDisplayAttribute->label = 'someNewLabel';
     $report->addDrillDownDisplayAttribute($drillDownDisplayAttribute);
     $this->assertTrue($report->canCurrentUserProperlyRenderResults());
 }
 public function testResolveReportToSavedReport()
 {
     $billy = User::getByUsername('billy');
     $report = new Report();
     $report->setDescription('aDescription');
     $report->setModuleClassName('ReportsTestModule');
     $report->setName('myFirstReport');
     $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
     $report->setOwner($billy);
     $report->setFiltersStructure('1 and 2 or 3');
     $report->setCurrencyConversionType(Report::CURRENCY_CONVERSION_TYPE_SPOT);
     $report->setSpotConversionCurrencyCode('CAD');
     $filter = new FilterForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
     $filter->attributeIndexOrDerivedType = 'string';
     $filter->value = 'aValue';
     $filter->operator = 'equals';
     $filter->availableAtRunTime = true;
     $report->addFilter($filter);
     $filter = new FilterForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
     $filter->attributeIndexOrDerivedType = 'currencyValue';
     $filter->value = 'aValue';
     $filter->secondValue = 'bValue';
     $filter->operator = 'between';
     $filter->currencyIdForValue = '4';
     $filter->availableAtRunTime = true;
     $report->addFilter($filter);
     $filter = new FilterForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
     $filter->attributeIndexOrDerivedType = 'owner__User';
     $filter->value = 'aValue';
     $filter->stringifiedModelForValue = 'someName';
     $filter->availableAtRunTime = false;
     $report->addFilter($filter);
     $filter = new FilterForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
     $filter->attributeIndexOrDerivedType = 'createdDateTime';
     $filter->value = 'aValue';
     $filter->secondValue = 'bValue';
     $filter->operator = null;
     $filter->currencyIdForValue = null;
     $filter->availableAtRunTime = true;
     $filter->valueType = 'Between';
     $report->addFilter($filter);
     $groupBy = new GroupByForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
     $groupBy->attributeIndexOrDerivedType = 'lastName';
     $groupBy->axis = 'y';
     $report->addGroupBy($groupBy);
     $orderBy = new OrderByForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
     $orderBy->attributeIndexOrDerivedType = 'url';
     $orderBy->order = 'desc';
     $report->addOrderBy($orderBy);
     $displayAttribute = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
     $displayAttribute->attributeIndexOrDerivedType = 'phone';
     $displayAttribute->label = 'someNewLabel';
     $report->addDisplayAttribute($displayAttribute);
     $drillDownDisplayAttribute = new DrillDownDisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
     $drillDownDisplayAttribute->attributeIndexOrDerivedType = 'firstName';
     $drillDownDisplayAttribute->label = 'someNewLabel';
     $report->addDrillDownDisplayAttribute($drillDownDisplayAttribute);
     $savedReport = new SavedReport();
     $this->assertNull($savedReport->serializedData);
     SavedReportToReportAdapter::resolveReportToSavedReport($report, $savedReport);
     $this->assertEquals('ReportsTestModule', $savedReport->moduleClassName);
     $this->assertEquals('myFirstReport', $savedReport->name);
     $this->assertEquals('aDescription', $savedReport->description);
     $this->assertEquals(Report::TYPE_ROWS_AND_COLUMNS, $savedReport->type);
     $this->assertEquals('1 and 2 or 3', $report->getFiltersStructure());
     $this->assertTrue($savedReport->owner->isSame($billy));
     $compareData = array('Filters' => array(array('availableAtRunTime' => true, 'currencyIdForValue' => null, 'value' => 'aValue', 'secondValue' => null, 'stringifiedModelForValue' => null, 'valueType' => null, 'attributeIndexOrDerivedType' => 'string', 'operator' => 'equals'), array('availableAtRunTime' => true, 'currencyIdForValue' => '4', 'value' => 'aValue', 'secondValue' => 'bValue', 'stringifiedModelForValue' => null, 'valueType' => null, 'attributeIndexOrDerivedType' => 'currencyValue', 'operator' => 'between'), array('availableAtRunTime' => false, 'currencyIdForValue' => null, 'value' => 'aValue', 'secondValue' => null, 'stringifiedModelForValue' => 'someName', 'valueType' => null, 'attributeIndexOrDerivedType' => 'owner__User', 'operator' => null), array('availableAtRunTime' => true, 'value' => 'aValue', 'secondValue' => 'bValue', 'stringifiedModelForValue' => null, 'valueType' => 'Between', 'attributeIndexOrDerivedType' => 'createdDateTime', 'operator' => null, 'currencyIdForValue' => null)), 'OrderBys' => array(array('order' => 'desc', 'attributeIndexOrDerivedType' => 'url')), 'GroupBys' => array(array('axis' => 'y', 'attributeIndexOrDerivedType' => 'lastName')), 'DisplayAttributes' => array(array('label' => 'someNewLabel', 'attributeIndexOrDerivedType' => 'phone', 'columnAliasName' => 'col0', 'queryOnly' => false, 'valueUsedAsDrillDownFilter' => false, 'madeViaSelectInsteadOfViaModel' => false)), 'DrillDownDisplayAttributes' => array(array('label' => 'someNewLabel', 'attributeIndexOrDerivedType' => 'firstName', 'columnAliasName' => 'col0', 'queryOnly' => false, 'valueUsedAsDrillDownFilter' => false, 'madeViaSelectInsteadOfViaModel' => false)));
     $unserializedData = unserialize($savedReport->serializedData);
     $this->assertEquals($compareData['Filters'], $unserializedData['Filters']);
     $this->assertEquals($compareData['OrderBys'], $unserializedData['OrderBys']);
     $this->assertEquals($compareData['GroupBys'], $unserializedData['GroupBys']);
     $this->assertEquals($compareData['DisplayAttributes'], $unserializedData['DisplayAttributes']);
     $this->assertEquals($compareData['DrillDownDisplayAttributes'], $unserializedData['DrillDownDisplayAttributes']);
     $this->assertEquals('1 and 2 or 3', $unserializedData['filtersStructure']);
     $this->assertEquals(Report::CURRENCY_CONVERSION_TYPE_SPOT, $unserializedData['currencyConversionType']);
     $this->assertEquals('CAD', $unserializedData['spotConversionCurrencyCode']);
     $saved = $savedReport->save();
     $this->assertTrue($saved);
 }
 /**
  * @param array $data
  * @param Report $report
  */
 protected static function resolveOrderBys($data, Report $report)
 {
     $report->removeAllOrderBys();
     $moduleClassName = $report->getModuleClassName();
     if (count($orderBysData = ArrayUtil::getArrayValue($data, ComponentForReportForm::TYPE_ORDER_BYS)) > 0) {
         foreach ($orderBysData as $key => $orderByData) {
             $orderBy = new OrderByForReportForm($moduleClassName, $moduleClassName::getPrimaryModelName(), $report->getType(), $key);
             $orderBy->setAttributes($orderByData);
             $report->addOrderBy($orderBy);
         }
     } else {
         $report->removeAllOrderBys();
     }
 }
 /**
  * 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);
 }