Create Plugin_Context class - #47
Conversation
felixarntz
left a comment
There was a problem hiding this comment.
@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 |
| * | ||
| * @test | ||
| */ | ||
| public function it_can_return_the_basename() { |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
See above, this could be test_path().
| * | ||
| * @test | ||
| */ | ||
| public function it_can_return_the_path_with_parameter() { |
There was a problem hiding this comment.
And this test_path_with_parameter().
| * @test | ||
| */ | ||
| public function it_can_return_the_path() { | ||
| $basename = $this->plugin_context->path(); |
There was a problem hiding this comment.
Variable name here and below is misleading, likely copy-paste oversight :)
mukeshpanchal27
left a comment
There was a problem hiding this comment.
Thanks @jjgrainger, Left nit-pick feedback.
| } | ||
|
|
||
| /** | ||
| * Returns the full URL for a pth relative to the plugin directory. |
There was a problem hiding this comment.
| * Returns the full URL for a pth relative to the plugin directory. | |
| * Returns the full URL for a path relative to the plugin directory. |
| $basename = $this->plugin_context->basename(); | ||
|
|
||
| $this->assertSame( $basename, 'plugin-check/plugin-check.php' ); |
There was a problem hiding this comment.
| $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.
mukeshpanchal27
left a comment
There was a problem hiding this comment.
Thanks @jjgrainger, LGTM!
felixarntz
left a comment
There was a problem hiding this comment.
@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.)
| 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' ) ); | ||
| } |
There was a problem hiding this comment.
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.
| 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' ) ); | ||
| } |
There was a problem hiding this comment.
Same here, how about using WP_PLUGIN_DIR . '/' . basename( dirname( __DIR__ ) ) instead of site_url( '/wp-content/plugins/plugin-check' )?
There was a problem hiding this comment.
@felixarntz Here we have to use WP_PLUGIN_URL instead of WP_PLUGIN_DIR
| public function test_basename() { | ||
| $this->assertSame( 'plugin-check/plugin-check.php', $this->plugin_context->basename() ); | ||
| } |
There was a problem hiding this comment.
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' ); |
mukeshpanchal27
left a comment
There was a problem hiding this comment.
@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' ); |
There was a problem hiding this comment.
| $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() ); |
There was a problem hiding this comment.
| $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() ); |
There was a problem hiding this comment.
| $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' ) ); |
There was a problem hiding this comment.
| $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() ); |
There was a problem hiding this comment.
| $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' ) ); |
There was a problem hiding this comment.
| $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' ) ); |
| $context = $this->plugin_main->context(); | ||
|
|
There was a problem hiding this comment.
| $context = $this->plugin_main->context(); |
Remove the variable.
| $context = $this->plugin_main->context(); | ||
|
|
||
| $this->assertIsObject( $context ); | ||
| $this->assertInstanceOf( Plugin_Context::class, $context ); |
There was a problem hiding this comment.
| $this->assertInstanceOf( Plugin_Context::class, $context ); | |
| $this->assertInstanceOf( Plugin_Context::class, $this->plugin_main->context() ); |
| 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' ) ); | ||
| } |
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
The plugin_dir_path( $this->main_file ) return plugin directory slug instead of the full relative path.
There was a problem hiding this comment.
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, '/' ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
* 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 <bordoni.dev@gmail.com>
Adds the
Plugin_ContextclassCloses #5