Skip to content

Commit

Permalink
fix:[DPMMA-2213] fix chart focus view statistics (mautic#13584)
Browse files Browse the repository at this point in the history
* fix:[DPMMA-2213] fix focus views stats on the chart

* fix:[DPMMA-2213] fix focus views stats on the chart - Functional UnitTest

* feat:[DPMMA-2213] add unittest for FocusModel class getStats method when dates params are null

* fix:[DPMMA-2213] remove testGetStatsWithNullDates.

* Update plugins/MauticFocusBundle/Tests/Model/FocusModelFunctionalTest.php

Co-authored-by: John Linhart <[email protected]>

* Update - clear commet plugins/MauticFocusBundle/Tests/Model/FocusModelFunctionalTest.php

Co-authored-by: John Linhart <[email protected]>

---------

Co-authored-by: John Linhart <[email protected]>
  • Loading branch information
AlanWierzchonCA and escopecz authored Apr 12, 2024
1 parent fade8e5 commit 27968c7
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 2 deletions.
4 changes: 2 additions & 2 deletions plugins/MauticFocusBundle/Model/FocusModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,12 @@ protected function dispatchEvent($action, &$entity, $isNew = false, Event $event
/**
* @param bool $canViewOthers
*/
public function getStats(Focus $focus, $unit, \DateTime $dateFrom = null, \DateTime $dateTo = null, $dateFormat = null, $canViewOthers = true): array
public function getStats(Focus $focus, $unit, \DateTime $dateFrom, \DateTime $dateTo, $dateFormat = null, $canViewOthers = true): array
{
$chart = new LineChart($unit, $dateFrom, $dateTo, $dateFormat);
$query = new ChartQuery($this->em->getConnection(), $dateFrom, $dateTo, $unit);

$q = $query->prepareTimeDataQuery('focus_stats', 'date_added', ['focus_id' => $focus->getId()]);
$q = $query->prepareTimeDataQuery('focus_stats', 'date_added', ['type' => Stat::TYPE_NOTIFICATION, 'focus_id' => $focus->getId()]);
if (!$canViewOthers) {
$this->limitQueryToCreator($q);
}
Expand Down
121 changes: 121 additions & 0 deletions plugins/MauticFocusBundle/Tests/Model/FocusModelFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare(strict_types=1);

namespace MauticPlugin\MauticFocusBundle\Tests\Model;

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\PageBundle\Entity\Hit;
use MauticPlugin\MauticFocusBundle\Entity\Focus;
use MauticPlugin\MauticFocusBundle\Entity\Stat;
use MauticPlugin\MauticFocusBundle\Model\FocusModel;

class FocusModelFunctionalTest extends MauticMysqlTestCase
{
private Lead $lead;

private FocusModel $focusModel;

protected function setUp(): void
{
parent::setUp();

$this->focusModel = static::getContainer()->get('mautic.focus.model.focus');
$this->lead = $this->createLead();
}

public function testGetStats(): void
{
$focusPopupA = $this->createFocus('popup focus A');
$focusStatExpected = $this->setTestsData($this->lead, $focusPopupA);

$to = new \DateTime('+1 day');
$from = new \DateTime('-1 month');

$focusStat = $this->focusModel->getStats($focusPopupA, null, $from, $to);

$focusViewsCount = array_sum($focusStat['datasets'][0]['data']);
$focusClickCount = array_sum($focusStat['datasets'][1]['data']);

$this->assertEquals($focusStatExpected['view'], $focusViewsCount);
$this->assertEquals($focusStatExpected['click'], $focusClickCount);
}

/**
* @return array<string, int>
*/
private function setTestsData(Lead $lead, Focus $focus): array
{
$hitPopupA = new Hit();
$hitPopupA->setLead($lead);

$this->focusModel->addStat($focus, Stat::TYPE_NOTIFICATION, null, $lead);
$this->focusModel->addStat($focus, Stat::TYPE_CLICK, $hitPopupA, $lead);
$this->focusModel->addStat($focus, Stat::TYPE_CLICK, $hitPopupA, $lead);
$this->focusModel->addStat($focus, Stat::TYPE_CLICK, $hitPopupA, $lead);
$this->focusModel->addStat($focus, Stat::TYPE_CLICK, $hitPopupA, $lead);

return ['view' => 1, 'click' => 4];
}

private function createFocus(string $name): Focus
{
$focus = new Focus();
$focus->setName($name);
$focus->setType('link');
$focus->setStyle('modal');
$focus->setProperties([
'bar' => [
'allow_hide' => 1,
'push_page' => 1,
'sticky' => 1,
'size' => 'large',
'placement' => 'top',
],
'modal' => [
'placement' => 'top',
],
'notification' => [
'placement' => 'top_left',
],
'page' => [],
'animate' => 0,
'link_activation' => 1,
'colors' => [
'primary' => '4e5d9d',
'text' => '000000',
'button' => 'fdb933',
'button_text' => 'ffffff',
],
'content' => [
'headline' => null,
'tagline' => null,
'link_text' => null,
'link_url' => null,
'link_new_window' => 1,
'font' => 'Arial, Helvetica, sans-serif',
'css' => null,
],
'when' => 'immediately',
'timeout' => null,
'frequency' => 'everypage',
'stop_after_conversion' => 1,
]);

$this->focusModel->saveEntity($focus);

return $focus;
}

private function createLead(): Lead
{
$lead = new Lead();
$lead->setFirstname('Contact');
$lead->setEmail('[email protected]');
$this->em->persist($lead);
$this->em->flush();

return $lead;
}
}

0 comments on commit 27968c7

Please sign in to comment.