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

SONARPHP-1559 Fix framework detection tests #1308

Merged
merged 1 commit into from
Nov 8, 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
2 changes: 1 addition & 1 deletion its/plugin/projects/drupal_project/test.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
use Drupal\something;

$variable = true
$variable = true;
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,31 @@

class FrameworkDetectionPHPTest {

private static final String DRUPAL_PROJECT_KEY = "drupal-project";

@RegisterExtension
public static OrchestratorExtension orchestrator = Tests.ORCHESTRATOR;
public static final OrchestratorExtension orchestrator = Tests.ORCHESTRATOR;

@Test
void shouldNotDetectIssueWhenFrameworkDetectionDisabled() {
Tests.provisionProject(DRUPAL_PROJECT_KEY, "Project with several extensions", "php", "it-profile");
SonarScanner build = createScanner()
.setProjectDir(Tests.projectDirectoryFor("drupal_project"))
.setProperty("sonar.php.frameworkDetection", "false");
Tests.executeBuildWithExpectedWarnings(orchestrator, build);

List<Issues.Issue> issues = Tests.issuesForComponent(DRUPAL_PROJECT_KEY);
var issues = scanDrupalProject("Drupal project 1", "drupal-project-1", false);
assertThat(issues).isEmpty();
}

@Test
void shouldDetectIssueWhenFrameworkDetectionDisabled() {
Tests.provisionProject(DRUPAL_PROJECT_KEY, "Project with several extensions", "php", "it-profile");
void shouldDetectIssueWhenFrameworkDetectionEnabled() {
var issues = scanDrupalProject("Drupal project 2", "drupal-project-2", true);
assertThat(issues).hasSize(1);
}

List<Issues.Issue> scanDrupalProject(String name, String key, boolean frameworkDetectionEnabled) {
Tests.provisionProject(key, name, "php", "drupal-profile");
SonarScanner build = createScanner()
.setProjectDir(Tests.projectDirectoryFor("drupal_project"))
.setProperty("sonar.php.frameworkDetection", "false");
.setProjectKey(key)
.setProjectName(name)
.setSourceEncoding("UTF-8")
.setSourceDirs(".")
.setProperty("sonar.php.frameworkDetection", "" + frameworkDetectionEnabled);
Tests.executeBuildWithExpectedWarnings(orchestrator, build);

List<Issues.Issue> issues = Tests.issuesForComponent(DRUPAL_PROJECT_KEY);
assertThat(issues).hasSize(1);
return Tests.issuesForComponent(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Tests {
// PHP Plugin
.addPlugin(PHP_PLUGIN_LOCATION)
.restoreProfileAtStartup(FileLocation.ofClasspath(RESOURCE_DIRECTORY + "profile.xml"))
.restoreProfileAtStartup(FileLocation.ofClasspath(RESOURCE_DIRECTORY + "drupal_profile.xml"))
.restoreProfileAtStartup(FileLocation.ofClasspath(RESOURCE_DIRECTORY + "no_rules.xml"))
// Custom rules plugin
// TODO Fix in SONARPHP-1520 Migrate Custom Rules module to Gradle
Expand Down Expand Up @@ -206,6 +207,10 @@ private static void assertAnalyzerLogs(String logs) {
class NestedCustomRulesTest extends CustomRulesTest {
}

@Nested
class NestedFrameworkDetectionPHPTest extends FrameworkDetectionPHPTest {
}

@Nested
class NestedNonPhpProjectTest extends NonPhpProjectTest {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<profile>
<name>drupal-profile</name>
<language>php</language>
<rules>
<rule>
<repositoryKey>php</repositoryKey>
<key>S1781</key>
</rule>
</rules>
</profile>