Closed (fixed)
Project:
Tamper
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
29 May 2018 at 22:36 UTC
Updated:
17 Jun 2020 at 20:59 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
jamesdixon commentedHere's some really rough progress, saving for use at another computer.
Comment #3
jamesdixon commentedFurther progress...
Comment #4
jamesdixon commentedHopefully patch file uploads this time...
Comment #5
jamesdixon commentedMore progress on this one, nearly done the plugin itself.
Comment #6
jamesdixon commentedNote to self, this isn't going to work since it's not a procedural function anymore.
Comment #7
jamesdixon commentedMade more progress here.
Comment #8
jamesdixon commentedThe difficulty I'm having with writing tests for this plugin, is in the original version, it generated a ton of settings values through the following function which occurs at the time of form validation. Is there a way to make a mock $form and $form_state object so I can run validateConfigurationForm() inside the tests? Appreciate your guidance.
Comment #9
jamesdixon commentedComment #10
megachrizYou could mock
FormStateInterfaceand in order to return a value for$form_state->getValue()you could let PHPUnit return a value map: https://phpunit.de/manual/6.5/en/test-doubles.html#test-doubles.stubs.ex...Example (untested):
An other option is to use Prophecy: https://github.com/phpspec/prophecy
I haven't used it much yet, but I believe it would work something like this:
As you see, it replaces
->method('methodName')with->methodName()as if you were actually calling the method. It could be that the order in which the method is called with each given value matters, I'm not sure.Finally, if a method tends to become very complex, it could be a good idea to put part of it into a new protected method. This way, you could write an unit test for just that piece of code in
validateConfigurationForm()that doesn't necessarily do validating. Not sure if it would be a good idea to that in this case as I see in the more complex part of the method also a call tosetErrorByName()on the form state object.Comment #11
jamesdixon commentedAwesome, thanks for the advice @MegaChriz! Very helpful, I'll give this a shot.
Comment #12
jamesdixon commentedI'm getting closer here is my latest progress. Having some trouble with validateConfigurationForm() I'll explain soon.
Comment #13
jamesdixon commentedThe trouble is $this->plugin->submitConfigurationForm() is not claling $this->plugin->validateConfigurationForm() at all. I have thrown var_dumps() inside the validate function and it's being ignored. Not sure, I tried validateForm() also but no luck. Is validateConfigurationForm() the correct function for form validation in D8?
Comment #14
megachrizSubmitting forms don't necessarily work in unit tests. If you call methods like
validateConfigurationForm()in unit tests, you want to purely test the code invalidateConfigurationForm(). The test method would then be calledtestValidateConfigurationForm(),testConfigurationForm()or alike. Feeds has some example unit tests for configuration forms. See the test classes in feeds/tests/src/Unit/Feeds/Fetcher/Form.It looks like that the test
testStriPosFilter()shouldn't be focussing on the form functions at all. Testing the Tamper plugins behavior when callingtamper()on it should focus on providing the Tamper configuration as it gets stored, and disregard the fact that these values get there via a form.Tests that test validating or submitting the form, should test what configuration gets stored based on the input values. Testing what
tamper()would then do is out of scope for that test.Comment #15
ericgsmith commentedI haven't take a thorough look but be caution of the way you are using setConfiguration.
This is from the ConfigurablePluginInterface - it is expected to be the whole configuration of the plugin, e.g. when the plugin is created. Its purpose is not for individual values, it is expected the whole configuration.
By setting an individual value, you are saying set this value and use the default values for everything else.
E.g instead of
It needs to be
Comment #16
jamesdixon commentedThanks @MegaChriz and @ericgsmith for the guidance. With your advice I hope to wrap this one up soon.
Comment #17
jamesdixon commentedOh man this one was a beast of a plugin to test!
Appreciate all your advice on this one guys. I am bracing for impact with the review. This plugin seems to have been built in a very different style than the others in D7.
Comment #19
jamesdixon commentedAh I need schema for hidden config options here, that will be quick
Comment #20
jamesdixon commentedLets try that again. Not sure if I got the "array" type for word_list correct in the yml as there were no examples I could find in the file.
Comment #21
jamesdixon commentedComment #28
jamesdixon commentedI think what I was looking for was a sequence type:
https://www.drupal.org/files/ConfigSchemaCheatSheet1.5.pdf
Lets give this a shot.
Comment #29
ericgsmith commentedThanks James - massive effort, looks like a fairly complex set of options to manage. I'm going to have a bit of a think about how the tamper method is being called, for now here is a few bits of minor feedback.
Needs the handles multiple annotation property
This can only work with an array of configuraiton, here it is passing in 5 arrays.
This has been removed in Drupal 8. See https://www.drupal.org/node/1992584 for the change record. We will need to work with the new Unicode class.
Typo - conf looks like it should be self
I'm not a fan of setting any configuration in the validation method.
Validation should be ensuring the all the form values are correct. If we need to massage or change any of the user input, I think its more common to put those values back into the form state object.
We should only be configuring the plugin in the submit handler.
Naming conventions for methods should reflect what this is doing, e.g. "match" - we don't need to prefix it with our module / plugin names.
Comment #30
vijay.mayilsamy commentedThanks for the feedback, Eric
@James, I have managed fix the above feedbacks except this one.
+++ b/src/Plugin/Tamper/KeywordFilter.php
@@ -0,0 +1,210 @@
+ $this->setConfiguration($config);
I'm not a fan of setting any configuration in the validation method.
Validation should be ensuring the all the form values are correct. If we need to massage or change any of the user input, I think its more common to put those values back into the form state object.
We should only be configuring the plugin in the submit handler.
Comment #31
mitrpaka commentedUpdated patch with changes in validation and submit handler to handle configuration settings in submit handler only.
Comment #32
volkswagenchickTagging for DrupalCamp Asheville
Comment #33
volkswagenchickTagging for the next to North American contrib days, Asheville and Colorado
DrupalCamp Asheville contrib days are July 13-14, 2019
DrupalCamp Colorado contrib day is Aug 4, 2019
Comment #34
andypostI'd like to set RTBC but constants needs better names and docs
It really hard to read "SETTINGS_" prefix and harder to type (use in code)
Also each one needs docs
Comment #35
MoCart commentedI tried patches #22 and #30 in DP 8.7.7 (feeds 8.x-3.0-alpha5, feeds_tamper 8.x-2.0-beta1, feeds_ex 8.x-1.0-alpha2), and something still seems to be missing.
I get error "method not found" for "feedsTamperKeywordFilterMatch". Is there another patch file for KeywordFilter.php with that function?
Also, I was getting a form validation error until I added “use Drupal\Component\Utility\Unicode;” to the header of KeywordFilter.php.
Comment #36
MoCart commentedOK, patch #31 works. You can disregard my #35 post.
Comment #37
mmaranao commentedHi guys, this looks like the filter I'm looking for but is there a way to return the key for the matched keyword instead of the values?
Thanks for your help!
Comment #38
jamesdixon commentedLets update the test to work with the new setup (ie: add that extra parameter when creating a new test object).
Then we can see if it passes.
Comment #39
jamesdixon commentedComment #40
zabej commented@jamesdixon made several changes here. Please have a look.
Comment #41
jamesdixon commentedThanks @zabej.
I see @andypost had some suggestions on improving the code base in #34
I'd say we need to:
1) Remove the SETTING_ part of all the PHP constants and make sure we don't miss any so the code functions the same.
2) I'm finding conflicting information on coding standards for documenting consts. I think this would be straight forward:
So above each const lets document what it is, using clues from the description of each form field.
For example:
Comment #42
andypostStandards for constants are
- outside of interface/class https://www.drupal.org/docs/develop/standards/coding-standards#s-constants
- inside should confirm https://www.drupal.org/node/2831620
Examples are all over core like https://git.drupalcode.org/project/drupal/-/blob/8.8.x/core/modules/node...
Comment #43
jamesdixon commentedThanks for directing us the the right docs @andypost.
For 2) We'd want to use this style:
Comment #44
zabej commentedHello @james,
Thanks for clarifications.
I've create one more changes package. Just a remark.
The three constants below has no any visual filter (in attachment https://www.drupal.org/files/issues/2020-05-28/admin-structure-feeds-man...). I also did not find them on tamper page. Please clarify where can I get to know the description. I add for regex something but not sure
Comment #45
jamesdixon commentedAfter removing math plugin updates here's what I recommend:
1) Looks like WORD_LIST is a computed value of SETTING_WORDS based on settings that were added.
We could do:
2) That REGEX settings is also calculated and not an exposed option.
3) The FUNCTION setting controls which function we're using (mb_stripos or mb_strpos)
These are all configuration option array indexes though, not sure if there's a better way to document them than describing what the config option is for.
Comment #46
jamesdixon commentedOkay I think it makes sense to add the following to the front of each of these:
Index for the word list configuration option.
Where we'd replace word list with whatever the option is. Then we add the description after. So a full example would be:
Comment #47
andypostRe-roll (math accepted), fix CS around consts and address last 2 comments
Also converted test to use data provider so all sets for test in one place
PS: sadly interdiff bugger then patch
Comment #48
zabej commentedHello @andypost
What do you mean under "PS: sadly interdiff bugger then patch" ?
Comment #49
andypost@zabej It means that reading the patch more productive then interdiff which is repeatable pattern
Comment #51
jamesdixon commented@andypost: thanks for recommending the docs cleanup and fixing that up.
Thanks everyone!
Looks good to me. Added to 8.x-1.x in commit e7c49aab7b718f1e5509546f6ec1a7df52fa20d4.