Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds test for PHPCS #91

Merged
merged 2 commits into from
Oct 15, 2024
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
43 changes: 43 additions & 0 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: PHPCS check

on: pull_request

permissions:
actions: read
checks: read
contents: read
deployments: none
issues: read
packages: none
pull-requests: read
repository-projects: none
security-events: none
statuses: read

jobs:
phpcs:
name: PHPCS
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
lfs: false
persist-credentials: false
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
tools: cs2pr
- name: Install dependencies
run:
composer init --name=matomo/anonymouspiwikusagemeasurement --quiet;
composer --no-plugins config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true -n;
composer config repositories.matomo-coding-standards vcs https://github.com/matomo-org/matomo-coding-standards -n;
composer require matomo-org/matomo-coding-standards:dev-master;
composer install --dev --prefer-dist --no-progress --no-suggest
- name: Check PHP code styles
id: phpcs
run: ./vendor/bin/phpcs --report-full --standard=phpcs.xml --report-checkstyle=./phpcs-report.xml
- name: Show PHPCS results in PR
if: ${{ always() && steps.phpcs.outcome == 'failure' }}
run: cs2pr ./phpcs-report.xml --prepend-filename
14 changes: 7 additions & 7 deletions AnonymousPiwikUsageMeasurement.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -16,8 +17,8 @@

class AnonymousPiwikUsageMeasurement extends \Piwik\Plugin
{
const TRACKING_DOMAIN = 'https://demo-anonymous.matomo.org';
const EXAMPLE_DOMAIN = 'http://example.com';
public const TRACKING_DOMAIN = 'https://demo-anonymous.matomo.org';
public const EXAMPLE_DOMAIN = 'http://example.com';

private $profilingStack = array();

Expand Down Expand Up @@ -79,11 +80,9 @@ public function trackApiCall(&$return, $endHookParams)

// we need to make sure the call was actually for this method to not send wrong data.
if ($method === $call['method']) {

$neededTimeInMs = ceil(($endTime - $call['time']) * 1000);
break;
}

} while (!empty($this->profilingStack));

if (empty($neededTimeInMs)) {
Expand Down Expand Up @@ -118,9 +117,11 @@ public function addMatomoClientTracking(&$out)
'userId' => Piwik::getCurrentUserLogin()
);

if (Piwik::isUserIsAnonymous()
if (
Piwik::isUserIsAnonymous()
|| !$settings->canUserOptOut->getValue()
|| $userSettings->userTrackingEnabled->getValue()) {
|| $userSettings->userTrackingEnabled->getValue()
) {
// an anonymous user is currently always tracked, an anonymous user would not have permission to read
// this user setting. The `isUserIsAnonymous()` check is not needed but there to improve performance
// in case user is anonymous. Then we avoid checking whether user has access to any sites which can be slow
Expand All @@ -135,5 +136,4 @@ public function addMatomoClientTracking(&$out)

$out .= "\nvar piwikUsageTracking = " . json_encode($config) . ";\n";
}

}
1 change: 1 addition & 0 deletions SystemSettings.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down
1 change: 1 addition & 0 deletions Tasks.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down
2 changes: 1 addition & 1 deletion Tracker.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -41,5 +42,4 @@ protected function getBaseUrl()

return parent::getBaseUrl();
}

}
5 changes: 2 additions & 3 deletions Tracker/CustomVariables.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -92,6 +93,4 @@ public function getClientVisitCustomVariables()
)
);
}


}
}
4 changes: 2 additions & 2 deletions Tracker/Profiles.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -58,5 +59,4 @@ public function uninstall()
{
Db::dropTables(array($this->tableNamePrefixed));
}

}
}
2 changes: 1 addition & 1 deletion Tracker/Targets.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -63,5 +64,4 @@ public function getTargets()

return $targets;
}

}
4 changes: 2 additions & 2 deletions Tracker/Trackers.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -45,5 +46,4 @@ public function makeTrackers()

return $trackers;
}

}
}
1 change: 1 addition & 0 deletions Updates/0.1.1.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down
1 change: 1 addition & 0 deletions Updates/4.0.0.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down
2 changes: 1 addition & 1 deletion UserSettings.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -48,5 +49,4 @@ private function createUsersOptOutSetting()
$field->description = 'If enabled, anonymous usage data will be tracked. For example which pages are viewed and which reports are used most often. For more information contact your system administrator.';
});
}

}
36 changes: 36 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<ruleset name="anonymousPiwikUsageMeasurement" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

<description>Matomo Coding Standard for AnonymousPiwikUsageMeasurement plugin</description>

<arg name="extensions" value="php" />

<file>.</file>

<exclude-pattern>tests/javascript/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>

<rule ref="Matomo"></rule>

<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="250" />
</properties>
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
<!-- Classnames for our update files don't match PascalCase, this can't be changed easily -->
<exclude-pattern>Updates/*</exclude-pattern>
</rule>

<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<!-- Allow using method name without camel caps in tests as long as some methods are named test_* -->
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<!-- Allow using multiple classes in one file for tests -->
<exclude-pattern>tests/*</exclude-pattern>
</rule>
</ruleset>
6 changes: 3 additions & 3 deletions tests/Fixtures/SendSystemReportTaskFixture.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

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

namespace Piwik\Plugins\AnonymousPiwikUsageMeasurement\tests\Fixtures;

use Piwik\API\Request;
Expand Down Expand Up @@ -66,7 +68,5 @@ private function executeSomeApiMethods()

$date = Date::factory('today')->toString();
Request::processRequest('CoreAdminHome.invalidateArchivedReports', array('idSites' => '1', 'period' => 'year', 'dates' => $date, 'cascadeDown' => '1'));

}

}
}
2 changes: 1 addition & 1 deletion tests/Integration/AnonymousPiwikUsageMeasurementTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -141,5 +142,4 @@ public function provideContainerConfig()
'Piwik\Access' => new FakeAccess()
);
}

}
3 changes: 1 addition & 2 deletions tests/Integration/SystemSettingsTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -20,7 +21,6 @@
*/
class SystemSettingsTest extends IntegrationTestCase
{

private $idSite = 1;

/**
Expand Down Expand Up @@ -135,5 +135,4 @@ public function test_customPiwikSiteId_shouldThrowAnException_IfValueIsNotNumeri
$this->expectDeprecationMessage("Site Id 'MyTest0' should be a number");
$this->settings->customPiwikSiteId->setValue('MyTest0');
}

}
2 changes: 1 addition & 1 deletion tests/Integration/Tracker/CustomVariablesTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -135,5 +136,4 @@ public function provideContainerConfig()
'Piwik\Access' => new FakeAccess()
);
}

}
3 changes: 1 addition & 2 deletions tests/Integration/Tracker/ProfilesTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -135,6 +136,4 @@ private function pushProfile($creationDate, $category = 'Category', $name = 'myN
{
$this->profiles->pushProfile($creationDate, $category, $name, $action, $count, $wallTime);
}


}
2 changes: 1 addition & 1 deletion tests/Integration/Tracker/TargetsTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -128,5 +129,4 @@ private function assertTargets($expectedTargets, $settings)

$this->assertSame($expectedTargets, $targets->getTargets());
}

}
8 changes: 3 additions & 5 deletions tests/Integration/Tracker/TrackersTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -13,8 +14,8 @@
use Piwik\Plugins\AnonymousPiwikUsageMeasurement\Tracker;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

class TestTargets extends Tracker\Targets {

class TestTargets extends Tracker\Targets
{
private $targets = array();

public function __construct($targets)
Expand All @@ -26,7 +27,6 @@ public function getTargets()
{
return $this->targets;
}

}

/**
Expand Down Expand Up @@ -84,7 +84,5 @@ public function test_makeTrackers_CanCreateMultipleTrackers_AndSavesUrlAndIdSite

$this->assertStringStartsWith('http://apache.piwik/piwik/piwik.php?idsite=101&rec=1', $trackers[1]->getUrlTrackPageView());
$this->assertStringEndsWith('url=https%3A%2F%2Fdemo-anonymous.matomo.org&urlref=', $trackers[1]->getUrlTrackPageView());

}

}
3 changes: 1 addition & 2 deletions tests/Integration/UserSettingsTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -22,7 +23,6 @@
*/
class UserSettingsTest extends IntegrationTestCase
{

private $idSite = 1;

/**
Expand Down Expand Up @@ -61,5 +61,4 @@ private function createUserSettingsWithUserOptOut($optOut)
$system->canUserOptOut->setValue($optOut);
return new UserSettings($system);
}

}
3 changes: 2 additions & 1 deletion tests/System/CheckDirectDependencyUseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Plugins\AnonymousPiwikUsageMeasurement\tests\System;

use Piwik\Plugins\TestRunner\Commands\CheckDirectDependencyUse;
Expand Down Expand Up @@ -44,4 +45,4 @@ public function testCommand()
],
], $checkDirectDependencyUse->usesFoundList[$pluginName]);
}
}
}
Loading
Loading