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
17 changes: 15 additions & 2 deletions Slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public function validateReportParameters(&$parameters, $reportType)
} elseif (empty($parameters[self::SLACK_CHANNEL_ID_PARAMETER])) {
throw new \Exception(Piwik::translate('Slack_SlackChannelIdRequiredErrorMessage'));
}
$slackChannels = explode(',', (string) $parameters[self::SLACK_CHANNEL_ID_PARAMETER]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this just use the validateCustomAlertReportParameters method? It's a little confusing that this appears to allow multiple channel IDs while validateCustomAlertReportParameters doesn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snake14 Files can be shared to multiple channelIDs by specifying comma separated values, but this does not work for sending a text message for Slack, hence we have 2 different checks

foreach ($slackChannels as $slackChannel) {
if (!ctype_alnum($slackChannel)) {
throw new \Exception(Piwik::translate('Slack_SlackChannelIdInvalidErrorMessage'));
}
}
}

/**
Expand Down Expand Up @@ -384,8 +390,15 @@ public function uninstall()
*/
public function validateCustomAlertReportParameters($parameters, $alertMedium)
{
if ($alertMedium === self::SLACK_TYPE && empty($parameters[self::SLACK_CHANNEL_ID_PARAMETER])) {
throw new \Exception(Piwik::translate('Slack_SlackChannelIdRequiredErrorMessage'));
if ($alertMedium === self::SLACK_TYPE) {
$settings = StaticContainer::get(SystemSettings::class);
if (empty($settings->slackOauthToken->getValue())) {
throw new \Exception(Piwik::translate('Slack_OauthTokenRequiredErrorMessage'));
} elseif (empty($parameters[self::SLACK_CHANNEL_ID_PARAMETER])) {
throw new \Exception(Piwik::translate('Slack_SlackChannelIdRequiredErrorMessage'));
} elseif (!ctype_alnum($parameters[self::SLACK_CHANNEL_ID_PARAMETER])) {
throw new \Exception(Piwik::translate('Slack_SlackChannelIdInvalidErrorMessage'));
}
}
}

Expand Down
1 change: 1 addition & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"OauthTokenSettingDescription": "Enter your Slack OAuth Token generated from your Slack. %1$sLearn more%2$s.",
"PleaseFindYourReport": "Here is your %1$s report for %2$s",
"SlackChannelIdRequiredErrorMessage": "Slack Channel ID cannot be empty.",
"SlackChannelIdInvalidErrorMessage": "Invalid Slack Channel ID, only alphanumeric values are allowed.",
"SlackChannel": "Slack Channel",
"SlackEnterYourSlackChannelIdHelpText": "Enter the Slack Channel ID of the channel that will receive these reports. To find the ID, go to Slack and open the channel details > About tab. %1$sLearn more%2$s",
"SlackAlertContent": "%1$s has been triggered for website %2$s as the metric %3$s in report %4$s %5$s."
Expand Down
37 changes: 36 additions & 1 deletion tests/Integration/CustomAlertsApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Piwik\Plugins\Slack\tests;

use Piwik\Container\StaticContainer;
use Piwik\Plugins\Slack\SystemSettings;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tests\Framework\TestingEnvironmentManipulator;
Expand Down Expand Up @@ -43,13 +45,41 @@ public function setUp(): void
$this->idSite = Fixture::createWebsite('2012-08-09 11:22:33');
}

public function testAddAlertShouldThrowExceptionIfSlackOauthTokenNotSet()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Slack_OauthTokenRequiredErrorMessage');
$this->addAlert('', false);
}

public function testAddAlertShouldThrowExceptionIfSlackChannelIdNotEmptyButOauthTokenNotSet()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Slack_OauthTokenRequiredErrorMessage');
$this->addAlert('channelID', false);
}

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

public function testAddAlertShouldThrowExceptionIfInvalidSlackChannelId()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Slack_SlackChannelIdInvalidErrorMessage');
$this->addAlert('ChanneldId1@123');
}

public function testAddAlertShouldThrowExceptionIfInvalidSlackChannelId2()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Slack_SlackChannelIdInvalidErrorMessage');
$this->addAlert('ChannelID1,ChannelID2');
}

public function testAddAlertSuccess()
{
$id = $this->addAlert($slackChannelId = 'channelID');
Expand All @@ -76,8 +106,13 @@ public function testUpdateAlertSuccess()
$this->assertEquals('channelIDNew', $alert['slack_channel_id']);
}

private function addAlert($slackChannelId = '')
private function addAlert($slackChannelId = '', $createToken = true)
{
if ($createToken) {
$settings = StaticContainer::get(SystemSettings::class);
$settings->slackOauthToken->setValue('test123');
}

return $this->api->addAlert(
'Test Slack and Email',
$this->idSite,
Expand Down
67 changes: 67 additions & 0 deletions tests/Integration/SlackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Piwik\Plugins\Slack\tests;

use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugins\ScheduledReports\API;
use Piwik\Plugins\ScheduledReports\ScheduledReports;
use Piwik\Plugins\Slack\Slack;
Expand Down Expand Up @@ -113,6 +114,72 @@ public function getAlertData()
];
}

public function testValidateReportParametersSlackTokenNotSetShouldThrowOauthException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Slack_OauthTokenRequiredErrorMessage');
$parameters = [ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_GRAPHS_ONLY];
Piwik::postEvent('ScheduledReports.validateReportParameters', [&$parameters, 'slack']);
}

public function testValidateReportParametersSlackTokenNotSetShouldThrowSlackChannelIdEmptyException()
{
$settings = StaticContainer::get(SystemSettings::class);
$settings->slackOauthToken->setValue('test123');
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Slack_SlackChannelIdRequiredErrorMessage');
$parameters = [ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_GRAPHS_ONLY];
Piwik::postEvent('ScheduledReports.validateReportParameters', [&$parameters, 'slack']);
}

public function testValidateReportParametersSlackTokenNotSetShouldThrowSlackChannelIdEmptyException2()
{
$settings = StaticContainer::get(SystemSettings::class);
$settings->slackOauthToken->setValue('test123');
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Slack_SlackChannelIdRequiredErrorMessage');
$parameters = [ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_GRAPHS_ONLY, Slack::SLACK_CHANNEL_ID_PARAMETER => ''];
Piwik::postEvent('ScheduledReports.validateReportParameters', [&$parameters, 'slack']);
}

public function testValidateReportParametersSlackTokenNotSetShouldThrowSlackChannelIdInvalidException()
{
$settings = StaticContainer::get(SystemSettings::class);
$settings->slackOauthToken->setValue('test123');
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Slack_SlackChannelIdInvalidErrorMessage');
$parameters = [ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_GRAPHS_ONLY, Slack::SLACK_CHANNEL_ID_PARAMETER => 'test123@11'];
Piwik::postEvent('ScheduledReports.validateReportParameters', [&$parameters, 'slack']);
}

public function testValidateReportParametersSlackTokenNotSetShouldThrowSlackChannelIdInvalidException2()
{
$settings = StaticContainer::get(SystemSettings::class);
$settings->slackOauthToken->setValue('test123');
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Slack_SlackChannelIdInvalidErrorMessage');
$parameters = [ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_GRAPHS_ONLY, Slack::SLACK_CHANNEL_ID_PARAMETER => 'test123,Demo@11'];
Piwik::postEvent('ScheduledReports.validateReportParameters', [&$parameters, 'slack']);
}

public function testValidateReportParametersSlackTokenShouldNotThrowAnyException()
{
$settings = StaticContainer::get(SystemSettings::class);
$settings->slackOauthToken->setValue('test123');
$parameters = [ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_GRAPHS_ONLY, Slack::SLACK_CHANNEL_ID_PARAMETER => 'test123'];
Piwik::postEvent('ScheduledReports.validateReportParameters', [&$parameters, 'slack']);
$this->assertNotEmpty($parameters);
}

public function testValidateReportParametersSlackTokenShouldNotThrowAnyException2()
{
$settings = StaticContainer::get(SystemSettings::class);
$settings->slackOauthToken->setValue('test123');
$parameters = [ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_GRAPHS_ONLY, Slack::SLACK_CHANNEL_ID_PARAMETER => 'test123,dEmoA,abcd123'];
Piwik::postEvent('ScheduledReports.validateReportParameters', [&$parameters, 'slack']);
$this->assertNotEmpty($parameters);
}

private function assertHasReport($login, $idSite)
{
$report = $this->getReport($login, $idSite);
Expand Down