Problem/Motivation
API page: https://api.drupal.org/api/drupal/modules%21simpletest%21drupal_web_test...
"Get a list files that can be used in tests."
Missing an 'of' surely.
Also, the docs for this could really do to explain where the files come from. It appears to generate them, but I'm not entirely sure where it puts them.
Additionally, the documentation of the function this calls in simpletest.module, is also lacking.
Proposed resolution
Fix the docs.
Remaining tasks
Commit.
Note that if #2512210: SimpleTest - WebTestBase method creates binary-text files when the intention was to create text files, and text file creation is broken gets committed first, we'll need to come back here and update the docs so that it doesn't say the 'text' files are actually 'binary-text' any more. This issue is documenting the current status quo only.
User interface changes
None.
API changes
None. Docs only.
Data model changes
None. Docs only.
Brief beta eval: This is just API docs.
| Comment | File | Size | Author |
|---|---|---|---|
| #73 | interdiff-2299361-68-73.txt | 941 bytes | trgreen17 |
| #73 | description_of-2299361-73.patch | 3.41 KB | trgreen17 |
| #68 | interdiff-2299361-65-68.txt | 787 bytes | pushpinderchauhan |
| #68 | description_of-2299361-68.patch | 3.42 KB | pushpinderchauhan |
| #65 | interdiff-2299361-63-65.txt | 1.01 KB | naveenvalecha |
Comments
Comment #1
jhodgdonSame in WebTestBase::drupalGetTestFiles() in Drupal 8.
https://api.drupal.org/api/drupal/core!modules!simpletest!src!WebTestBas...
Looking at the code, it appears that the files go into the public files directory (which is set to something special during tests). Some files are generated, but some come from core/modules/simpletest/files.
Comment #2
joachim commented> Looking at the code, it appears that the files go into the public files directory (which is set to something special during tests).
What's confusing is that this method is used to generate files to test file upload fields. So you're uploading files from public:// that then are uploaded and saved by the field .... right back into public:// !!!
Comment #3
jhodgdonI have no idea about that. Anyway I do not think we need to document where the files are, because the return value gives you their paths.
Comment #4
joachim commentedWell that requires a developer to either read the whole of the function code, or do a trial run of it to see where the files end up!
Comment #5
jhodgdonBut why does a developer need to know this information ahead of time? That seems rather pointless. The point of this function is to return a list of the files, abstracting away the details so the developer doesn't need to know where they are, because this function lists them, right??!?
Comment #6
jhodgdonI'm resurrecting this issue.
What needs to be done, in my opinion:
a) Fix the grammar/typos in the method documentation.
b) Fix the documentation so it is accurate: it should say that it is generating files. The current docs imply it is just "getting a list", not that it is creating new files.
Comment #7
wadmiraal commentedOn it.
Comment #8
wadmiraal commentedSeriously, the $size parameter is almost useless, and used in only one, single test in Core. The problem is you need to know exactly what sizes are available in the test files. I'm listing them for clarity (and because checking the tests/files folder, as suggested, is a pain), but it's really not intuitive. It would be better return files that are larger than the passed size, or just leave the size parameter out completely (like marking it as deprecated, or discouraged).
Anyway, patch attached.
Comment #9
jhodgdonOK... looks pretty good!
That list of file sizes could be more concise. Maybe something like:
(I totally made up those numbers, but get the idea?)
I think that would be better, because the list is kind of long and in kind of a random order... I mean it is in order by size but probably for a given test you'd be interested in just one type of file anyway, and it's hard to scan to see what sizes are actually available for one particular type. Thoughts?
Other than that, looks fine to me.
Comment #10
wadmiraal commentedGood point. On it.
Comment #11
wadmiraal commentedComment #12
jhodgdonMuch nicer!
A couple of very minor points:
"can be" seems a bit odd to me. Maybe just say:
The files generated by this function are of the following sizes...
Technically we require a serial comma in Drupal docs, so:
... 2048, and 20480
Comment #13
wadmiraal commentedHow's this?
Comment #14
jhodgdonLooks good to me, thanks!
Comment #15
xjmOverall this looks great. Two comments:
The expression "a bunch" is informal. Could we say "numerous" or "several"... or better, exactly how many?
Comment #16
jhodgdonGood idea! So I guess the plan would be:
a) Put the docs about generating files into simpletest_generate_files().
b) In drupalGetTestFiles(), mostly just say it returns the files generated by simplest_generate_files(), plus anything else that someone drops in there.
Comment #17
trgreen17 commentedFollowed jhodgdon's advice above, divided documentation between the two files. Patch and interdiff attached.
Comment #18
jhodgdonThanks for the new patch! I don't think this is quite what we were after, however.
The function simpletest_generate_file() takes as input the name of the file to create. So we shouldn't be putting anything in there about what the file names are; that part comes from the calling method WebTestBase::drupalGetTestFiles().
So. Let's go back to the patch in #13 and also look at the existing docs for simpletest_generate_file()... Let's do this (starting from that patch):
a) Add something to the docs for simpletest_generate_file(), in the @param $type docs, that explains what is actually put into the file for each value of $type: for text it looks like it's random ASCII characters. For binary, it's random characters whose codes are in the range of 0 to 31. For binary-text, it's a random sequence of 0 and 1 values. Also explicitly say what the default value is (binary-text).
b) Add something to the docs for @param $filename on simpletest_generate_file() that explains that .txt is appended to the supplied file name and the file is put in the public file directory.
c) Leave the first line of WebTestBase::drupalGetTestFiles() alone -- it was good in the patch of #13.
d) The first paragraph of the method:
Change this to say something like: The first time this method is called, it will call simpletest_generate_file() to generate text and binary-text files in the public:// directory. It will also copy all files in ...
The reason for this change is that technically this method is not passing 'text' as the type to simpletest_generate_file(), so it will be generating the default binary-text type of file, not 'text', in:
That could be a bug, but fixing it is out of scope for this documentation issue.
e)
Let's change this to say "filenames" instead of "files".
f)
Let's change the last two lines to say something like "The files generated and copied by this function..." to be more accurate.
g)
Looking at simpletest_generate_files() and the files that are currently in core/modules/simpletest/files, this is not accurate. The ones generated will all have .txt extensions, so in this function it will generate text-*.txt and binary-*.txt, so I don't know where that "no extension" idea comes from. And in the HTML line, there is also html-*.txt.
Thanks!
Comment #19
trgreen17 commentedThanks for the detailed explanation, it is much appreciated. I think I understand all of your points and I will attempt a proper fix as soon as possible.
Comment #20
trgreen17 commentedHi @jhodgdon,
So sorry it took me so long to get back on this but I have the whole day to work on it and would love to get this issue closed! I'm new at contrib though and have a question if you don't mind: You asked me to modify the docs for simpletest_generate_file(). Should that go within the function: "protected function drupalGetTestFiles..." (line 499 of Web TestBase.php) or should it go above that function with the other docs?
Thanks!
Tim
Comment #21
trgreen17 commentedHi @jhodgdon,
Sorry, never mind. I see that I should be putting this in the simpletest.module file, around line 490! My bad. :(
Tim
Comment #22
trgreen17 commentedHi @jhodgdon and @xjm, here is my latest effort for this patch and interdiff. I tried to follow jhodgdon's excellent advice above as closely as possible, but please note that I added a couple things myself in WebTestBase.php:
Line 466 - added "and have appropriate extensions:"
Line 470 - added serial comma after "image-*.jpg"
Comment #23
jmolivas commentedComment #26
trgreen17 commentedI recreated the patch to fix errors.
Comment #27
jhodgdonThanks! This is very close. Just a few cosmetic things to fix:
Documentation lines need to wrap as close to 80-character lines as possible, without going over. The first line is too short (at least, I think "to" will fit there), and the second is too long.
These lines also need rewrapping into one paragraph of less than 80 character lines.
I think also for "binary" it should say "...random characters whose codes are in the range..." ?
And we can shorten the 'default' bit, something like:
If $type is "binary-text" (default), a random ...
[note: no word "and" in the binary-text sentence]
Re-wrap.
Also I think we don't need "as well" at the end of the second sentence, since it starts with "... will also". :)
Comment #28
trgreen17 commentedThanks for the tips, it is helpful to know the proper form and style! Working on this today.
Comment #29
trgreen17 commentedAgain, thank you for clearing up the 80 characters in documentation for me. I was doing it by what 'looks' best, I guess from my very old DTP days. :-) So I set phpStorm to show me 80 character columns rather than 120, which made it easy! Attaching the new patch and interdiff now.
NOTE: Please ignore the files above. I named them wrong because I didn't refresh the page before I got the suggested patch name. Rookie mistake. I will re-roll the files and post them with the proper file names. Thanks for your patience!
Comment #30
trgreen17 commentedLatest patch and interdiff, re-rolled and named correctly.
Comment #31
jhodgdonI'm not too hung up on patch names. ;)
Anyway, this is looking good! But unfortunately, I double-checked and it's not quite accurate:
It looks like the code in the generate files function is actually calling simpletest_generate_file() with 'binary' for the files whose names start with 'binary', and no arg (which defaults to 'binary-text', not 'text') for the files whose names start with 'text'.
So we should document this correctly. In this line it should say it generates binary and binary-text types, and down below I think we should note that the text-*.txt files are actually binary-text.
And we should possibly also file a follow-up issue to change this, because it looks like the intention may have been that the "text" files contained random ASCII characters? I do not know.... ???
The binary files generated are actually of size 64 and 1024.
The binary-text files are actually: 16, 256, 1024, 2048, 20480 -- and again note that these are binary-text not really "text".
Comment #32
trgreen17 commentedWow, thank you for catching these things! I could have checked more carefully... Working on it now.
Comment #33
trgreen17 commentedNewest patch and interdiff per instructions above. Thanks for the note about patch names. Understood, but I will still try to name them correctly. :-)
NOTE: We will need someone with more experience than I have to determine if a new issue should be created around "it looks like the intention may have been that the "text" files contained random ASCII characters?"
Comment #34
trgreen17 commentedNeeds review.
Comment #35
trgreen17 commentedActually @jhodgdon, I think I get what you're saying about the followup issue. If we can determine that what you said is correct, and text files should contain ASCII characters, I might be able to tackle it.
This issue is my first effort at contrib (and thanks for all the help!) but it would also be very exciting to contribute some actual code to core. What is your recommendation for this?
Comment #36
trgreen17 commentedIf this is actually the desired behavior (ASCII characters), could it be as simple as replacing line 518 of simpletest.module:
$text .= chr(rand(32, 126));with some form of$text .= string chr ( int $ascii )? Or maybe even some form of thefprintf()function?Comment #37
jhodgdonNo, what I think needs to be done is that the method in WebTestBase should pass 'text' in as the type, instead of getting the default of 'binary-text'. Right? I think that the function in simpletest.module is clear enough, and now it's actually documented (once we get this patch in). But WebTestBase, in my opinion, is not calling it correctly.
Anyway, the first step there would be to write up an issue with a clear summary of the problem, which I think is that that WebTestBase method is generating binary-text files instead of text files, and it looks like probably text was the intention.
Meanwhile, I believe that the current patch clearly documents the current code. Thanks!
Comment #38
trgreen17 commentedRe the new issue: of course you are right about how to do it, your way sounds much simpler. I will write up the new issue tomorrow morning, and if it's okay with you, PM you the URL so you can see if I did it correctly?
Re this issue: Awesome!! Thanks so much for your help. :-) As I said this is my first contrib and it's actually very exciting! So... what's next for this issue? Do I need to do anything else?
Thanks again,
Tim
Comment #39
jhodgdonPlease post the issue URL here in this issue rather than messaging me in IRC. And use the "relationships" section on the issue to mark it as "related" to this issue. Thanks!
And for this issue, you're done unless it gets marked back to "needs work". At least until it is committed to Drupal 8 -- then someone will need to backport the patch to Drupal 7 (but wait until D8 has been taken care of before that, just in case it still is deemed to need work).
Comment #40
trgreen17 commentedThanks for such clear explanations and help with this issue. I will create the new issue later today or first thing tomorrow, post the URL here, relate it back to this issue, and try to solve it myself!
And I am following this issue of course, so if there is any more work needed I will be alerted and will be happy to do it.
Comment #41
trgreen17 commentedI have created a related issue to force text files to be created as 'text' rather than the default of 'binary-text'
Here is the issue URL: https://www.drupal.org/node/2512210
Comment #42
trgreen17 commentedComment #43
jhodgdonWe need to fix this a bit -- these are not the right sizes for the generated files:
For binary and text, these numbers are the number of *lines* being generated, not the number of bytes in the file.
Comment #44
neetu morwani commentedComment #45
neetu morwani commentedComment #47
trgreen17 commentedChanging 'bytes' to 'lines' in WebTestBase.php, documentation line #488, since this is more accurate after fixing the related issue.
Comment #48
jhodgdonThanks! But...
This is possibly accurate, but not useful. The caller has to pass in the file size in bytes to match, but we only tell them the number of lines and not how many bytes are in each line. How would they figure this out?
Plus I think maybe that the other files (the ones that are copied not generated) are given as file sizes? Unsure...
Anyway I don't think this is a great way to document it. If they have to match file size, we should either tell them the file sizes, or leave this list out entirely.
Comment #49
trgreen17 commentedGood point, seems like too much documentation in this case? I'm thinking of leaving it out since as you said they need to pass the file sizes in. How about this as the last line of that doc block?
* @param $size
* (optional) File size in bytes to match. Defaults to NULL, which will not
* filter the returned list by size.
Thoughts?
Comment #50
jhodgdonWorks for me.
Comment #51
trgreen17 commentedExcellent. Here's a patch with "less is more" documentation. :-)
Comment #52
jhodgdonThanks! This looks fine to me now.
Note that if #2512210: SimpleTest - WebTestBase method creates binary-text files when the intention was to create text files, and text file creation is broken gets committed first, we'll need to come back here and update the docs so that it doesn't say the 'text' files are actually 'binary-text' any more.
Updated summary and added brief beta eval.
Comment #53
jhodgdonComment #54
xjm#51 looks very good to me, but maybe we should postpone this on #2512210: SimpleTest - WebTestBase method creates binary-text files when the intention was to create text files, and text file creation is broken since that one is also RTBC? Thanks!
Comment #55
jhodgdonThe other issue got in. This patch needs a bit of an update now. See comment #52.
Comment #56
trgreen17 commentedYes, will work on it this afternoon.
Comment #57
trgreen17 commentedI changed "binary-text" to "text" in one place and removed what I consider to be unnecessary documentation on one line. But do you think we should say "ASCII text" instead of just "text?"
Comment #58
jhodgdonASCII text sounds like a good idea to me. Also one other thought/question:
Do you think we should also put '0' and '1' in quotes here, to avoid it being mistaken for real binary numbers, or is it clear enough because the type is "binary-text"?
Comment #59
trgreen17 commentedAgreed on both ASCII and '0' and '1' in quotes. It will make it more clear. However, to possibly avoid another patch, should I use single or double quotes for the 0 and 1? We are using double quotes for "text" but from a PHP perspective maybe single quotes are better for the numbers?
Comment #60
jhodgdonGenerally, our Drupal coding standards prefer single quotes in code, so we should also use them in docs. I would change all of them to single quotes. Thanks!
Comment #61
trgreen17 commentedMakes perfect sense! Happy to do it.
Comment #62
jhodgdonGreat!
So I took one more look at this patch and since it's just documentation at this point, I found a few nitpick things to fix to make the docs just a bit better:
If we're fixing up the docs, can we put "a" in here? :)
These are not really "examples", but actually the three allowed values.
And would this be clearer as a list? Something like:
(optional) The type, one of:
- text: The generated file contains random ASCII characters.
...
We use serial commas in Drupal docs by convention, so needs a comma after JavaScript in this line.
Comment #63
naveenvalechaaddressed #62
Comment #64
jhodgdonThanks! Pretty close but:
These two descriptions are garbled.
Comment #65
naveenvalechaComment #67
jhodgdonThanks for the new patch! But:
These are still garbled.
Either say "contains" or "are inserted into the file", but not both. Either way is fine with me.
Comment #68
pushpinderchauhan commentedIncorporated #67 suggestion.
Comment #71
naveenvalechaComment #72
jhodgdonOK, now the text looks good, thanks!
Unfortunately (sorry I didn't notice this before), the indentation is two spaces too much. List markers - are lined up with the text above.
Comment #73
trgreen17 commentedThanks for the patches! I figured I would go ahead and implement #72 above.
Comment #74
jhodgdonGreat, thanks! I think this is ready to go in now.
Comment #75
xjmThanks for reworking this one after the other patch.
So this parameter is... bizarre. It's especially fussy since you have to use an exact size matching a file generated by
simpletest_generate_file()... which doesn't even generate based on size; it generates based on lines and characters! Looking above, it seems a similar concern was raised in #8.I checked and it is used three times, once directly and twice when passed to the wrapper method in
FileFieldTestBase::getTestFile(). Somewhere since #15 we lost the previous docs about what sizes were available... looks like that was done deliberately in #51. I can see the case for that. However, reading the current docs, I don't know how to find out what file sizes there are as we have also lost the prompt in HEAD to check the temporary directory (at least I think that's what it means).TBH it's code smell and it seems like the parameter should possibly be moved to the calling code, but that's out of scope. I think what we should do is file a followup to deprecate the size parameter and replace the test usages (both beta-eligible changes) since the docs here are an improvement overall.
This issue only changes documentation, so per https://www.drupal.org/core/beta-changes, this can be completed any time during the Drupal 8 beta phase. Committed and pushed to 8.0.x. Thanks everyone for figuring out how this kinda strange API works and documenting it!
Comment #77
jhodgdonBizarre indeed. Follow-up:
#2534800: \Drupal\Tests\TestFileCreationTrait has mostly unusable file size filter
Also we need to backport these docs fixes to Drupal 7.
Comment #81
stefan.r commentedTagging the backport to D7 as a novice task -- just note that not necessarily all of the code will apply (for instance, "core/modules/simpletest/files" is "modules/simpletest/files" in D7.
Prior to backporting we should also manually determine that all of the D8 comments apply to D7 as well.
Comment #82
MaskyS commentedHere I go ;)
Comment #83
MaskyS commentedComment #84
MaskyS commentedComment #87
mile23