-
Notifications
You must be signed in to change notification settings - Fork 83
Create Plugin_Context class #47
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
Conversation
felixarntz
left a comment
There was a problem hiding this 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).
includes/Plugin_Context.php
Outdated
| /** | ||
| * Class representing the context in which the plugin is running. | ||
| * | ||
| * @since 0.1.0 |
There was a problem hiding this comment.
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.
tests/Plugin_Context_Tests.php
Outdated
| * | ||
| * @test | ||
| */ | ||
| public function it_can_return_the_basename() { |
There was a problem hiding this comment.
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).
tests/Plugin_Context_Tests.php
Outdated
| * | ||
| * @test | ||
| */ | ||
| public function it_can_return_the_path() { |
There was a problem hiding this comment.
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().
tests/Plugin_Context_Tests.php
Outdated
| * | ||
| * @test | ||
| */ | ||
| public function it_can_return_the_path_with_parameter() { |
There was a problem hiding this comment.
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().
tests/Plugin_Context_Tests.php
Outdated
| * @test | ||
| */ | ||
| public function it_can_return_the_path() { | ||
| $basename = $this->plugin_context->path(); |
There was a problem hiding this comment.
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 :)
mukeshpanchal27
left a comment
There was a problem hiding this 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.
includes/Plugin_Context.php
Outdated
| } | ||
|
|
||
| /** | ||
| * Returns the full URL for a pth relative to the plugin directory. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| * Returns the full URL for a pth relative to the plugin directory. | |
| * Returns the full URL for a path relative to the plugin directory. |
tests/Plugin_Context_Tests.php
Outdated
| $basename = $this->plugin_context->basename(); | ||
|
|
||
| $this->assertSame( $basename, 'plugin-check/plugin-check.php' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jjgrainger, LGTM!
felixarntz
left a comment
There was a problem hiding this 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.)
| 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.
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.
| 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.
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' )?
There was a problem hiding this comment.
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
| public function test_basename() { | ||
| $this->assertSame( 'plugin-check/plugin-check.php', $this->plugin_context->basename() ); | ||
| } |
There was a problem hiding this comment.
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.
tests/Plugin_Context_Tests.php
Outdated
| 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.
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?
mukeshpanchal27
left a comment
There was a problem hiding this 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.
tests/Plugin_Context_Tests.php
Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $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' ); |
tests/Plugin_Context_Tests.php
Outdated
| } | ||
|
|
||
| public function test_basename() { | ||
| $this->assertSame( 'plugin-check/plugin-check.php', $this->plugin_context->basename() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $this->assertSame( 'plugin-check/plugin-check.php', $this->plugin_context->basename() ); | |
| $this->assertSame( $this->plugin_name . '/plugin-check.php', $this->plugin_context->basename() ); |
tests/Plugin_Context_Tests.php
Outdated
| } | ||
|
|
||
| public function test_path() { | ||
| $this->assertSame( 'plugin-check/', $this->plugin_context->path() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $this->assertSame( 'plugin-check/', $this->plugin_context->path() ); | |
| $this->assertSame( $this->plugin_name . '/', $this->plugin_context->path() ); |
tests/Plugin_Context_Tests.php
Outdated
| } | ||
|
|
||
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $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' ) ); |
tests/Plugin_Context_Tests.php
Outdated
| } | ||
|
|
||
| public function test_url() { | ||
| $this->assertSame( site_url( '/wp-content/plugins/plugin-check/' ), $this->plugin_context->url() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $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() ); |
tests/Plugin_Context_Tests.php
Outdated
| } | ||
|
|
||
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $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' ) ); |
tests/Plugin_Main_Tests.php
Outdated
| $context = $this->plugin_main->context(); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $context = $this->plugin_main->context(); |
Remove the variable.
tests/Plugin_Main_Tests.php
Outdated
| $context = $this->plugin_main->context(); | ||
|
|
||
| $this->assertIsObject( $context ); | ||
| $this->assertInstanceOf( Plugin_Context::class, $context ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $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.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, '/' ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
* 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]>
Adds the
Plugin_ContextclassCloses #5