diff --git a/MicrosoftTeamsApi.php b/MicrosoftTeamsApi.php index b495826..512f855 100644 --- a/MicrosoftTeamsApi.php +++ b/MicrosoftTeamsApi.php @@ -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') diff --git a/plugin.json b/plugin.json index 87a7a4f..a681c6c 100644 --- a/plugin.json +++ b/plugin.json @@ -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": [ { diff --git a/tests/Integration/CustomAlertsApiTest.php b/tests/Integration/CustomAlertsApiTest.php new file mode 100644 index 0000000..8a863a1 --- /dev/null +++ b/tests/Integration/CustomAlertsApiTest.php @@ -0,0 +1,139 @@ +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 + ); + } +} diff --git a/tests/Integration/CustomAlertsTest.php b/tests/Integration/CustomAlertsTest.php new file mode 100644 index 0000000..eacb0c9 --- /dev/null +++ b/tests/Integration/CustomAlertsTest.php @@ -0,0 +1,43 @@ +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() + ); + } +} diff --git a/tests/UI/CustomAlerts_spec.js b/tests/UI/CustomAlerts_spec.js new file mode 100644 index 0000000..39a32d4 --- /dev/null +++ b/tests/UI/CustomAlerts_spec.js @@ -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); + }); + + }); \ No newline at end of file diff --git a/tests/UI/expected-ui-screenshots/CustomAlerts_empty_report.png b/tests/UI/expected-ui-screenshots/CustomAlerts_empty_report.png new file mode 100644 index 0000000..13409b5 Binary files /dev/null and b/tests/UI/expected-ui-screenshots/CustomAlerts_empty_report.png differ diff --git a/tests/UI/expected-ui-screenshots/CustomAlerts_new_custom_alert.png b/tests/UI/expected-ui-screenshots/CustomAlerts_new_custom_alert.png new file mode 100644 index 0000000..701dd5c Binary files /dev/null and b/tests/UI/expected-ui-screenshots/CustomAlerts_new_custom_alert.png differ diff --git a/tests/UI/expected-ui-screenshots/CustomAlerts_send_via_teams_new.png b/tests/UI/expected-ui-screenshots/CustomAlerts_send_via_teams_new.png new file mode 100644 index 0000000..a30d775 Binary files /dev/null and b/tests/UI/expected-ui-screenshots/CustomAlerts_send_via_teams_new.png differ diff --git a/tests/UI/expected-ui-screenshots/CustomAlerts_teams_alert_report_save_success.png b/tests/UI/expected-ui-screenshots/CustomAlerts_teams_alert_report_save_success.png new file mode 100644 index 0000000..c3d53e5 Binary files /dev/null and b/tests/UI/expected-ui-screenshots/CustomAlerts_teams_alert_report_save_success.png differ diff --git a/tests/UI/expected-ui-screenshots/CustomAlerts_teams_report_enabled_default.png b/tests/UI/expected-ui-screenshots/CustomAlerts_teams_report_enabled_default.png new file mode 100644 index 0000000..0eb2f5e Binary files /dev/null and b/tests/UI/expected-ui-screenshots/CustomAlerts_teams_report_enabled_default.png differ diff --git a/tests/UI/expected-ui-screenshots/CustomAlerts_teams_report_error.png b/tests/UI/expected-ui-screenshots/CustomAlerts_teams_report_error.png new file mode 100644 index 0000000..1fa810b Binary files /dev/null and b/tests/UI/expected-ui-screenshots/CustomAlerts_teams_report_error.png differ