Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MicrosoftTeamsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function getTeamsDriveId(string $siteID): string
* @param array $additionalHeaders
* @param $requestBodyAsString
* @param $httpMethod
* @return array|int[]|string
* @return string
* @throws \Exception
*/
private function sendHttpRequest(string $url, int $timeout, ?array $requestBody, array $additionalHeaders = [], $requestBodyAsString = false, $httpMethod = 'POST')
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "5.0.0",
"theme": false,
"require": {
"matomo": ">=5.6.0-b1,<6.0.0-b1"
"matomo": ">=5.7.0-alpha,<6.0.0-b1"
},
"authors": [
{
Expand Down
139 changes: 139 additions & 0 deletions tests/Integration/CustomAlertsApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Plugins\MicrosoftTeams\tests;

use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tests\Framework\TestingEnvironmentManipulator;
use Piwik\Plugins\CustomAlerts\API;

/**
* @group MicrosoftTeams
* @group CustomAlertsApiTest
* @group Plugins
*/
class CustomAlertsApiTest extends IntegrationTestCase
{
/**
* @var \Piwik\Plugins\CustomAlerts\API
*/
protected $api;

protected $idSite;

public function setUp(): void
{
self::$fixture->extraPluginsToLoad = array('CustomAlerts');
TestingEnvironmentManipulator::$extraPluginsToLoad = self::$fixture->extraPluginsToLoad;

parent::setUp();

$pluginManager = \Piwik\Plugin\Manager::getInstance();
$pluginManager->loadPlugin('CustomAlerts');
$pluginManager->installLoadedPlugins();
$pluginManager->activatePlugin('CustomAlerts');
$this->api = API::getInstance();
$this->idSite = Fixture::createWebsite('2012-08-09 11:22:33');
}

public function testAddAlertShouldThrowExceptionIfSMicrosoftTeamsWebhookUrlEmpty()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('MicrosoftTeams_IncomingWebhookRequiredErrorMessage');
$this->addAlert('');
}

public function testAddAlertShouldThrowExceptionIfInvalidMicrosoftTeamsWebhookUrl()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('MicrosoftTeams_IncomingWebhookInvalidErrorMessage');
$this->addAlert('webhookURL');
}

public function testAddAlertSuccess()
{
$id = $this->addAlert($msTeamsWebhookUrl = 'https://webhook.microsft.com/webhook');
$this->assertEquals(1, $id);
$alert = $this->api->getAlert($id);
$this->assertEquals(['email','teams'], $alert['report_mediums']);
$this->assertEquals($msTeamsWebhookUrl, $alert['ms_teams_webhook_url']);
}

public function testUpdateAlertShouldThrowExceptionIfEmptyMicrosoftTeamsWebhookUrl()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('MicrosoftTeams_IncomingWebhookRequiredErrorMessage');
$idAlert = $this->addAlert('https://webhook.microsft.com/webhook');
$this->updateAlert($idAlert);
}

public function testUpdateAlertShouldThrowExceptionIfInvalidMicrosoftTeamsWebhookUrl()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('MicrosoftTeams_IncomingWebhookInvalidErrorMessage');
$idAlert = $this->addAlert('https://webhook.microsft.com/webhook');
$this->updateAlert($idAlert, 'webhookURL');
}

public function testUpdateAlertSuccess()
{
$idAlert = $this->addAlert($msTeamsWebhookUrl = 'https://webhook.microsft.com/webhook');
$this->updateAlert($idAlert, $msTeamsWebhookUrl . 'new');
$alert = $this->api->getAlert($idAlert);
$this->assertEquals(['email','teams'], $alert['report_mediums']);
$this->assertEquals('https://webhook.microsft.com/webhooknew', $alert['ms_teams_webhook_url']);
}

private function addAlert($msTeamsWebhookUrl = '')
{

return $this->api->addAlert(
'Test teams and Email',
$this->idSite,
'day',
1,
[],
[],
'nb_visits',
'less_than',
$metricMatched = 5,
1,
'MultiSites_getOne',
'matches_exactly',
'Piwik',
['email', 'teams'],
'',
$msTeamsWebhookUrl
);
}

private function updateAlert($idAlert, $msTeamsWebhookUrl = '')
{
return $this->api->editAlert(
$idAlert,
'Test teams and Email',
$this->idSite,
'day',
1,
[],
[],
'nb_visits',
'less_than',
$metricMatched = 5,
1,
'MultiSites_getOne',
'matches_exactly',
'Piwik',
['email', 'teams'],
'',
$msTeamsWebhookUrl
);
}
}
43 changes: 43 additions & 0 deletions tests/Integration/CustomAlertsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Plugins\MicrosoftTeams\tests;

use Piwik\Plugins\CustomAlerts\CustomAlerts;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tests\Framework\TestingEnvironmentManipulator;

/**
* @group MicrosoftTeams
* @group CustomAlertsTest
* @group Plugins
*/

class CustomAlertsTest extends IntegrationTestCase
{
public function setUp(): void
{
self::$fixture->extraPluginsToLoad = array('CustomAlerts');
TestingEnvironmentManipulator::$extraPluginsToLoad = self::$fixture->extraPluginsToLoad;

parent::setUp();

$pluginManager = \Piwik\Plugin\Manager::getInstance();
$pluginManager->loadPlugin('CustomAlerts');
$pluginManager->installLoadedPlugins();
$pluginManager->activatePlugin('CustomAlerts');
}
public function testGetReportMediumOptions()
{
$this->assertContains(
['key' => 'teams', 'value' => 'CustomAlerts_MediumMicrosoftTeams', 'disabled' => false],
CustomAlerts::getReportMediumOptions()
);
}
}
86 changes: 86 additions & 0 deletions tests/UI/CustomAlerts_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*!
* Matomo - free/libre analytics platform
*
* Screenshot integration tests.
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

describe("CustomAlerts", function () {
this.fixture = "Piwik\\Tests\\Fixtures\\EmptySite";

// required to ensure no provider is set initially
this.optionsOverride = {
'persist-fixture-data': false,
};

async function captureScreen(screenshotName, theTest, selector) {
if (!selector) {
selector = reportSelector;
}
await theTest();
await page.waitForNetworkIdle();
await page.waitForSelector(selector);
expect(await page.screenshotSelector(selector)).to.matchImage(screenshotName);
}

before(function () {
testEnvironment.pluginsToLoad = ['CustomAlerts', 'MicrosoftTeams'];
testEnvironment.save();
});

it('should load the custom alerts as empty', async function () {
const selector = '.page';
await captureScreen('empty_report', async () => {
await page.goto('?module=CustomAlerts&action=index&idSite=1&period=day&date=yesterday');
}, selector);
});

it('should show load a new alert add screen', async function () {
const selector = '.page';
await captureScreen('new_custom_alert', async () => {
await page.evaluate(() => $('.icon-add').click());
}, selector);
});

it('should show send alert via MicrosoftTeams as an option enabled', async function () {
const selector = '.page';
await captureScreen('send_via_teams_new', async () => {
await page.click('.report-mediums .select-dropdown');
await page.waitForNetworkIdle();
await page.waitForTimeout(350); // wait for animation
}, selector);
await page.click('.report-mediums .select-dropdown');
});

it('should show MicrosoftTeams webhookURL input as enabled by default as it requires webhook URL only', async function () {
const selector = '.page';
await captureScreen('teams_report_enabled_default', async () => {
await page.evaluate(() => $('.report-mediums .select-wrapper ul li:contains("Teams")').click());
}, selector);
});

it('should show show error if webhookURL not set', async function () {
const selector = '.page';
await captureScreen('teams_report_error', async () => {
await page.type('#alertName', 'Test teams Alert');
await page.evaluate(() => $('.conditionAndValue .select-wrapper ul li:last').click());
await page.type('#metricValue', '2');
await page.click('.matomo-save-button input.btn');
await page.waitForNetworkIdle();
}, selector);
});

it('should save a report successfully', async function () {
const selector = '.page';
testEnvironment.configOverride.MicrosoftTeams = {teamsClientID: 'clientID', teamsClientSecret: 'clientSecret', teamsTenantID: 'tenantID', teamsTeamID: 'teamID'};
testEnvironment.save();
await captureScreen('teams_alert_report_save_success', async () => {
await page.type('input#webhookURL', 'https://WEBHOOK_URL');
await page.click('.matomo-save-button input.btn');
await page.waitForNetworkIdle();
}, selector);
});

});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.