Skip to content

Conversation

@jjgrainger
Copy link
Contributor

@jjgrainger jjgrainger commented Dec 19, 2022

Adds the Plugin_Context class

Closes #5

@jjgrainger jjgrainger added [Type] Enhancement A suggestion for improvement of an existing feature Infrastructure Issues for the overall plugin infrastructure Milestone 1 labels Dec 19, 2022
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@jjgrainger Left a few comments on the code here that isn't in #44 or #46 (mostly similar feedback though).

/**
* Class representing the context in which the plugin is running.
*
* @since 0.1.0
Copy link
Member

Choose a reason for hiding this comment

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

See my comments on #46, let's use n.e.x.t.

*
* @test
*/
public function it_can_return_the_basename() {
Copy link
Member

Choose a reason for hiding this comment

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

See my comments on #44 and #46 regarding usage of test_ prefix (applies throughout this class). The convention is to just call the method like the tested method (i.e. here test_basename()), unless there are multiple test methods for the same method in which case the method should be more descriptive (e.g. test_basename_returns_basename(), though here that is a pointless example).

*
* @test
*/
public function it_can_return_the_path() {
Copy link
Member

Choose a reason for hiding this comment

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

See above, this could be test_path().

*
* @test
*/
public function it_can_return_the_path_with_parameter() {
Copy link
Member

Choose a reason for hiding this comment

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

And this test_path_with_parameter().

* @test
*/
public function it_can_return_the_path() {
$basename = $this->plugin_context->path();
Copy link
Member

Choose a reason for hiding this comment

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

Variable name here and below is misleading, likely copy-paste oversight :)

Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

Thanks @jjgrainger, Left nit-pick feedback.

}

/**
* Returns the full URL for a pth relative to the plugin directory.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Returns the full URL for a pth relative to the plugin directory.
* Returns the full URL for a path relative to the plugin directory.

Comment on lines 18 to 20
$basename = $this->plugin_context->basename();

$this->assertSame( $basename, 'plugin-check/plugin-check.php' );
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$basename = $this->plugin_context->basename();
$this->assertSame( $basename, 'plugin-check/plugin-check.php' );
$this->assertSame( 'plugin-check/plugin-check.php', $this->plugin_context->basename() );

As per the assertSame documentation the expected should be in first place. Also remove variable.

Do similar change for other tests.

@jjgrainger jjgrainger marked this pull request as ready for review December 21, 2022 09:12
Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

Thanks @jjgrainger, LGTM!

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@jjgrainger Overall LGTM, however I have a few suggestions for making the tests more reliable, as they currently use hard-coded strings which I suspect will fail depending on how the test environment is set up. (They work here, but they may not work if someone sets up tests in another way, e.g. without Docker.)

Comment on lines 21 to 27
public function test_path() {
$this->assertSame( 'plugin-check/', $this->plugin_context->path() );
}

public function test_path_with_parameter() {
$this->assertSame( 'plugin-check/another/folder', $this->plugin_context->path( '/another/folder' ) );
}
Copy link
Member

Choose a reason for hiding this comment

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

To make these two checks more stable, I would suggest not hard-coding an assumed path here. It'd be more reliable to use dirname( __DIR__ ) instead of plugin-check.

Comment on lines 29 to 35
public function test_url() {
$this->assertSame( site_url( '/wp-content/plugins/plugin-check/' ), $this->plugin_context->url() );
}

public function test_url_with_parameter() {
$this->assertSame( site_url( '/wp-content/plugins/plugin-check/folder/file.css' ), $this->plugin_context->url( '/folder/file.css' ) );
}
Copy link
Member

Choose a reason for hiding this comment

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

Same here, how about using WP_PLUGIN_DIR . '/' . basename( dirname( __DIR__ ) ) instead of site_url( '/wp-content/plugins/plugin-check' )?

Copy link
Member

Choose a reason for hiding this comment

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

@felixarntz Here we have to use WP_PLUGIN_URL instead of WP_PLUGIN_DIR

Comment on lines 17 to 19
public function test_basename() {
$this->assertSame( 'plugin-check/plugin-check.php', $this->plugin_context->basename() );
}
Copy link
Member

Choose a reason for hiding this comment

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

Technically, even here we could make it more reliable by using basename( dirname( __DIR__ ) ) instead of the plugin-check directory. We know for sure the main file is called plugin-check.php, but the directory name depends on the WordPress setup and where the plugin is installed to.

public function set_up() {
parent::set_up();

$this->plugin_context = new Plugin_Context( 'plugin-check/plugin-check.php' );
Copy link
Member

Choose a reason for hiding this comment

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

Can we apply #47 (comment) changes here also?

Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

@jjgrainger Over to you for review of the changes and merging.

public function set_up() {
parent::set_up();

$this->plugin_context = new Plugin_Context( 'plugin-check/plugin-check.php' );
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$this->plugin_context = new Plugin_Context( 'plugin-check/plugin-check.php' );
$this->plugin_name = basename( dirname( __DIR__ ) );
$this->plugin_context = new Plugin_Context( $this->plugin_name . '/plugin-check.php' );

}

public function test_basename() {
$this->assertSame( 'plugin-check/plugin-check.php', $this->plugin_context->basename() );
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$this->assertSame( 'plugin-check/plugin-check.php', $this->plugin_context->basename() );
$this->assertSame( $this->plugin_name . '/plugin-check.php', $this->plugin_context->basename() );

}

public function test_path() {
$this->assertSame( 'plugin-check/', $this->plugin_context->path() );
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$this->assertSame( 'plugin-check/', $this->plugin_context->path() );
$this->assertSame( $this->plugin_name . '/', $this->plugin_context->path() );

}

public function test_path_with_parameter() {
$this->assertSame( 'plugin-check/another/folder', $this->plugin_context->path( '/another/folder' ) );
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$this->assertSame( 'plugin-check/another/folder', $this->plugin_context->path( '/another/folder' ) );
$this->assertSame( $this->plugin_name . '/another/folder', $this->plugin_context->path( '/another/folder' ) );

}

public function test_url() {
$this->assertSame( site_url( '/wp-content/plugins/plugin-check/' ), $this->plugin_context->url() );
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$this->assertSame( site_url( '/wp-content/plugins/plugin-check/' ), $this->plugin_context->url() );
$this->assertSame( WP_PLUGIN_URL . '/' . $this->plugin_name . '/', $this->plugin_context->url() );

}

public function test_url_with_parameter() {
$this->assertSame( site_url( '/wp-content/plugins/plugin-check/folder/file.css' ), $this->plugin_context->url( '/folder/file.css' ) );
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$this->assertSame( site_url( '/wp-content/plugins/plugin-check/folder/file.css' ), $this->plugin_context->url( '/folder/file.css' ) );
$this->assertSame( WP_PLUGIN_URL . '/' . $this->plugin_name . '/folder/file.css', $this->plugin_context->url( '/folder/file.css' ) );

Comment on lines 19 to 20
$context = $this->plugin_main->context();

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$context = $this->plugin_main->context();

Remove the variable.

$context = $this->plugin_main->context();

$this->assertIsObject( $context );
$this->assertInstanceOf( Plugin_Context::class, $context );
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$this->assertInstanceOf( Plugin_Context::class, $context );
$this->assertInstanceOf( Plugin_Context::class, $this->plugin_main->context() );

@jjgrainger jjgrainger merged commit 8561820 into trunk Dec 22, 2022
@jjgrainger jjgrainger deleted the feature/create-plugin-context-class branch December 22, 2022 11:32
Comment on lines +22 to +28
public function test_path() {
$this->assertSame( $this->plugin_name . '/', $this->plugin_context->path() );
}

public function test_path_with_parameter() {
$this->assertSame( $this->plugin_name . '/another/folder', $this->plugin_context->path( '/another/folder' ) );
}
Copy link
Member

Choose a reason for hiding this comment

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

@jjgrainger Just spotting this post-merge: This still doesn't fully address my original feedback: I would think we need to use WP_PLUGIN_DIR . '/' . $this->plugin_name instead of just $this->plugin_name. Have you tried that, were there any problems? At least in theory this still looks unstable to me without that bit.

Let me know what you think. If my concerns are valid, we can fix this throughout in a quick follow up PR.

cc @mukeshpanchal27

Copy link
Member

Choose a reason for hiding this comment

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

The plugin_dir_path( $this->main_file ) return plugin directory slug instead of the full relative path.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks both, I've addressed these comments in #51

* @return string Absolute path.
*/
public function path( $relative_path = '/' ) {
return plugin_dir_path( $this->main_file ) . ltrim( $relative_path, '/' );
Copy link
Member

Choose a reason for hiding this comment

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

Should update this line with return plugin_dir_path( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH ) . $this->main_file . ltrim( $relative_path, '/' ); to get full relative path. If we use this and run the unit test it gives Use of undefined constant WP_PLUGIN_CHECK_PLUGIN_DIR_PATH - assumed 'WP_PLUGIN_CHECK_PLUGIN_DIR_PATH' (this will throw an Error in a future version of PHP) notice that needs to figure out.

@jjgrainger I'd like to get your feedback on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @mukeshpanchal27

I've addressed this in #51. We're unable to use the WP_PLUGIN_CHECK_PLUGIN_DIR_PATH as it's created in the plugin-check.php file which is not bootstrapped as part of the tests (nor should it be).

The line here works correctly as the full path is expected to be passed in the constructor (see the `plugin-check.php file here).

I've updated the tests based on the feedback in #51 which I believe to be correct.

bordoni added a commit that referenced this pull request Sep 13, 2023
* Add an initial Trademark check. This is based entirely upon the code currently used within the Plugin Directory upload.

* Add unit test

* Fix the tests; use a singular needle.

* Tests: Test the slugs.

* Correct the portmanteau check, add test.

* Tests: Fully install all dependencies.

* Typo

* Ensure that for-use exceptions don't miss it being used earlier in the name

* Even if a plugin is using the valid for-use exception, check for other trademarks they may not be observing.

* Reduce cognitive load for is_valid_for_use_exception and include the rest of the docblock

* Remove verifications against the email since this data is not public + Code Standards and Docblocks for the whole file.

* Add back into the code references to code that will only run when used in the Plugins Repository site

* Include validation to only happen if the plugin is being reviewed.

---------

Co-authored-by: Gustavo Bordoni <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Infrastructure Issues for the overall plugin infrastructure [Type] Enhancement A suggestion for improvement of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create Plugin_Context class

4 participants