I've always thought the naming was poor, not sure if it was adapted from SimpleTest library or what, but randomName() doesn't seem right.

I propose changing the method to randomString().

If I can get some agreement I will write a patch that changes all the references.

Possibly remove the prefix parameter as it is unnecessary and almost never used.

Comments

mlncn’s picture

Status: Postponed (maintainer needs more info) » Active

Agreed.

An aside: Should there be a random text function (that puts in spaces and such)?

damien tournoud’s picture

While we are at it, it could make sense to have a randomHtml function, that produce simple valid HTML (like some <i> and <strong>).

boombatower’s picture

I'll work on a patch.

robloach’s picture

Status: Active » Needs review
StatusFileSize
new71.4 KB

I'm getting all passing tests.

webchick’s picture

I'm not so sure of this patch, and not just because I don't want to confuse test authors by shifting APIs out from under them this late into the game when we have over 111 test cases for D7. Rob Loach's patch makes it clear that this touches just about every test in core, so it's seriously going to mess people up if it changes.

But since we don't care about backwards compatibility in a code thaw, instead take a look at this:

     $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';

That's the defined charset for randomName(). This makes sense; those characters are the only ones allowed in usernames, for example. They're also guaranteed to be safe and not contain any weird characters that could mess something up.

On the other hand, if I saw a function called randomString(), I would expect it to generate things like ® and $ and ' and not be limited to a particular character set.

So I think the name of this function is actually fine, as it describes what it does. If we want to add a randomString() function additionally, that purposely attempts to generate wacky characters to see if things screw up, then that's another matter.

robloach’s picture

Status: Needs review » Needs work

The addition of a randomString function with a larger charset is a good idea.

boombatower’s picture

Status: Needs work » Needs review
StatusFileSize
new7.2 KB

Talked with webchick and added a few thoughts myself.

This patch leaves the randomName() method, but updates its code as it previously made sure strings started with something other than a number. Seems unnecessary.

Adds randomString() which includes misc. characters including space as requested.

Removes prefix as parameter and just prefixes strings with the database prefix. I would encourage people to use longer random strings instead of prefixes to ensure the strings are unique. The random functions should be just that...random.

Ran aggregator, profile, and tracker tests which I removed prefix code from....they still passes. If someone has time to run whole suite that would be great.

boombatower’s picture

StatusFileSize
new7.05 KB

Re-rolled.

Anonymous’s picture

Status: Needs review » Needs work

The last submitted patch failed testing.

boombatower’s picture

Status: Needs work » Needs review
StatusFileSize
new9.11 KB

Updated patch...interestingly there are now 377 references to randomName().

Status: Needs review » Needs work

The last submitted patch failed testing.

boombatower’s picture

Status: Needs work » Needs review
StatusFileSize
new11.82 KB

Had to do with the length of the random strings since they are now appended with $db_prefix. I added code to remove the redundant "simpletest" portion of the prefix to shorten them...and cleared out some suffixes used in field tests.

Status: Needs review » Needs work

The last submitted patch failed testing.

boombatower’s picture

Status: Needs work » Postponed
StatusFileSize
new11.82 KB

Actual bug in the field test. #446878: Field.test contains incorrect appending syntax

NOTE: accidentally re-uploaded patch :)

boombatower’s picture

Status: Postponed » Needs review

Other patch committed.

Status: Needs review » Needs work

The last submitted patch failed testing.

boombatower’s picture

Status: Needs work » Needs review
StatusFileSize
new12.39 KB

Interesting little quirk in the upload test, fixed.

dries’s picture

Status: Needs review » Fixed

Committed to CVS HEAD. Thanks.

c960657’s picture

randomString() is limited to US-ASCII characters. Any reason why it doesn't use a wider selection of Unicode characters?

sun’s picture

Status: Fixed » Needs work
+   * Generates a random string of ASCI characters of codes 32 to 126. That

Spot the typo here.

boombatower’s picture

Status: Needs work » Needs review
StatusFileSize
new699 bytes

Nice catch.

#19 I suppose not. Perhaps a follow up patch?

sun’s picture

Status: Needs review » Needs work
   /**
-   * Generates a random string.
+   * Generates a random string of ASCI characters of codes 32 to 126. That
+   * includes alpha-numeric characters and common misc characters.
    *
...
   /**
+   * Generates a random string containing letters, both capital and lower, and
+   * numbers. This method is better for restricted inputs that do not accept
+   * certain characters.

Function summaries should be on one line. If required, add a blank PHPDoc line after the summary, followed by a longer function description.

+   *   Length of random string to generate which will be appended to $db_prefx.

Another typo in here. (also in the second PHPDoc)

boombatower’s picture

Status: Needs work » Needs review
StatusFileSize
new1.36 KB

I have never even thought about the single line summary. I'll try and remember that for future patches.

I fixed the summary and such, but I'm not sure what the type is in the second block.

sun’s picture

Status: Needs review » Needs work

We're almost there.

   /**
-   * Generates a random string of ASCI characters of codes 32 to 126. That
-   * includes alpha-numeric characters and common misc characters.
+   * Generates a random string of ASCI characters of codes 32 to 126.
+   * The generated string includes alpha-numeric characters and common misc
+   * characters.
    *

There needs to be a blank PHPDoc line between the PHPDoc summary and PHPDoc description.

+   * The letters may be upper of lower case. This method is better for
+   * restricted inputs that do not accept certain characters.
    *
    * @param $length
    *   Length of random string to generate which will be appended to $db_prefx.

The typo is in $db_prefx. However, now you introduced another one in The letters may be upper of lower case.

Also, the fix from #21 is no longer contained.

boombatower’s picture

Status: Needs work » Needs review
StatusFileSize
new1.65 KB

Wasn't sure on new-line...fixed.

other typos fixed.

Hopefully this is good. :)

dries’s picture

Status: Needs review » Fixed

Great job, sun. Thanks! Keep 'em coming if you have more.

webchick’s picture

Status: Fixed » Needs work

Hm. Sorry, but I still think this needs work.

It's not at all clear from the documentation why you should use randomString() vs. why you should use randomName(). Can we please add a line to each (probably as well as a cross-referenced @see entry) to explain this?

webchick’s picture

Issue tags: +Needs documentation

Additionally, let's make sure all of the examples at http://drupal.org/simpletest that need to be updated are, and a mention of this API change on the 6.x -> 7.x updates page.

boombatower’s picture

Status: Needs work » Needs review
StatusFileSize
new1.58 KB

I added a bit of documentation.

boombatower’s picture

webchick’s picture

Status: Needs review » Fixed

Awesome. Thanks!

Status: Fixed » Closed (fixed)
Issue tags: -Needs documentation

Automatically closed -- issue fixed for 2 weeks with no activity.