Skip to content

Commit

Permalink
Fix code and tests for Multiple Domains configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Sep 12, 2022
1 parent 7829428 commit 6ee0896
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
25 changes: 17 additions & 8 deletions WPForms/Sniffs/PHP/ValidateDomainSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,17 @@ public function process( File $phpcsFile, $stackPtr ) {
$currentDomain = $this->getCurrentDomain( $phpcsFile, $stackPtr );
$expectedDomain = $this->getExpectedDomain( $phpcsFile );

if ( ! $expectedDomain ) {
if ( $currentDomain === false ) {
$phpcsFile->addError(
'Domain name must be string.',
$stackPtr,
'NotStringDomain'
);

return;
}

if ( ! $currentDomain || ! $expectedDomain ) {
return;
}

Expand All @@ -113,7 +123,7 @@ public function process( File $phpcsFile, $stackPtr ) {

$phpcsFile->addError(
sprintf(
"You are using invalid domain name. Use '%s' instead of '%s'",
"You are using invalid domain name. Use '%s' instead of '%s'.",
$expectedDomain,
$currentDomain
),
Expand All @@ -130,7 +140,7 @@ public function process( File $phpcsFile, $stackPtr ) {
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
* @param int $stackPtr The position in the PHP_CodeSniffer file's token stack where the token was found.
*
* @return string
* @return string|false
*/
private function getCurrentDomain( $phpcsFile, $stackPtr ) {

Expand All @@ -148,7 +158,7 @@ private function getCurrentDomain( $phpcsFile, $stackPtr ) {

// Last argument is not a string, but something else.
if ( $lastArgument ) {
return '';
return false;
}

$lastArgument = $phpcsFile->findNext( T_CONSTANT_ENCAPSED_STRING, $commaPtr + 1, $closePtr );
Expand Down Expand Up @@ -192,10 +202,9 @@ private function getExpectedDomain( $phpcsFile ) {
return $basename;
}

$relativeDir = str_replace( [ '\\', '/', '.' ], [ '-', '-', '' ], dirname( $filePath ) );
$relativeDir = strtolower( trim( $relativeDir, '-' ) );
preg_match( '/[\w.-]+/', dirname( $filePath ), $domain );

return $relativeDir ? $relativeDir : $basename;
return ! empty( $domain[0] ) ? strtolower( $domain[0] ) : $basename;
}

/**
Expand All @@ -209,7 +218,7 @@ private function getExpectedDomain( $phpcsFile ) {
*/
private function findDomainByProperty( $filePath ) {

$fileDir = dirname( $filePath );
$fileDir = DIRECTORY_SEPARATOR . ltrim( dirname( $filePath ), DIRECTORY_SEPARATOR );
$currentDomain = '';
$currentPath = '';

Expand Down
20 changes: 9 additions & 11 deletions WPForms/Tests/TestFiles/PHP/ValidateDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,29 @@

__( 'Invalid', 'wpforms' );
_e( 'Invalid', 'wpforms-geolocation' );
_x( 'Invalid', 'context', 'wpforms-lite' );
esc_html__( 'Invalid', 'wpforms-lite' );
esc_html_e( 'Invalid', 'wpforms-lite' );
esc_html_x( 'Invalid', 'context', 'wpforms-lite' );
esc_attr__( 'Invalid', 'wpforms-lite' );
esc_attr_e( 'Invalid', 'wpforms-lite' );
_x( 'Valid for lite only', 'context', 'wpforms-lite' );
esc_html__( 'Valid for lite only', 'wpforms-lite' );
esc_html_e( 'Valid for lite only', 'wpforms-lite' );
esc_html_x( 'Valid for lite only', 'context', 'wpforms-lite' );
esc_attr__( 'Valid for lite only', 'wpforms-lite' );
esc_attr_e( 'Valid for lite only', 'wpforms-lite' );
esc_attr_x( 'Invalid', 'context', 'wpforms' );
_n( 'Invalid', 'Invalid', 10, 'wpforms' );
_ex( 'Invalid', 'Invalid', 'wpforms' );
_nx( 'Invalid', 'Invalid', 10, 'Invalid', 'wpforms' );


// Additional two cases of valid syntax when text domain is not specified.
// Valid syntax when the text domain is not specified.
esc_html__( 'Valid' );

// Not a string as the last argument.
// Invalid - not a string as the last argument.
$bulk_counts['read'] = 25;
_n( '%d entry.', '%d entries.', $bulk_counts['read'] );

// Nested parenthesis with domain specified as the last argument.
// Invalid - nested parenthesis inside the gettext function with domain specified as the last argument.
_n(
'Found <strong>%s entry</strong>',
'Found <strong>%s entries</strong>',
absint( count( $this->entries->items ) ),
'wpforms'
);

// phpcs:enable WordPress.Security.EscapeOutput.UnsafePrintingFunction
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<config name="multi_domains" value="true"/>
<rule ref="WPForms.PHP.ValidateDomain">
<properties>
<property name="wpforms-lite" value="WPForms/"/>
<property name="wpforms-lite" value="WPForms"/>
</properties>
</rule>
</ruleset>
7 changes: 4 additions & 3 deletions WPForms/Tests/Tests/PHP/ValidateDomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function testProcess() {

$phpcsFile = $this->process( new ValidateDomainSniff() );

$this->fileHasErrors( $phpcsFile, 'InvalidDomain', [ 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 34, 38, 41 ] );
$this->fileHasErrors( $phpcsFile, 'InvalidDomain', [ 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 41 ] );
$this->fileHasErrors( $phpcsFile, 'NotStringDomain', [ 38 ] );
}

/**
Expand All @@ -33,7 +34,7 @@ public function testProcessWithMultiDomains() {

$phpcsFile = $this->process( new ValidateDomainSniff(), 'MultiDomains' );

$this->fileHasErrors( $phpcsFile, 'InvalidDomain', [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 21, 22, 23, 24, 25, 26, 34, 38 ] );
$this->fileHasErrors( $phpcsFile, 'InvalidDomain', [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 21, 22, 23, 24, 25, 26 ] );
}

/**
Expand All @@ -45,6 +46,6 @@ public function testProcessWithRewrittenPaths() {

$phpcsFile = $this->process( new ValidateDomainSniff(), 'ValidateDomainWithRewrites' );

$this->fileHasErrors( $phpcsFile, 'InvalidDomain', [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19, 20, 27, 28, 29, 30, 34, 38, 41 ] );
$this->fileHasErrors( $phpcsFile, 'InvalidDomain', [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19, 20, 27, 28, 29, 30, 41 ] );
}
}

0 comments on commit 6ee0896

Please sign in to comment.