Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quietone created an issue. See original summary.

quietone’s picture

quietone’s picture

Status: Active » Needs review

And let's test it.

mallezie’s picture

This looks good to me.
I did some minor code style changes.
Can confirm plugin works, and would be nice addition.
Tests look good as well.
Since i only did some code style changes, i'll probably can RTBC it, if the bot is happy.

penyaskito’s picture

Status: Needs review » Needs work

This looks great!

+++ b/core/modules/migrate/tests/src/Unit/process/SubstrTest.php
@@ -0,0 +1,71 @@
+    $this->assertSame('Captain', $value);

Should we remove this one, as it will be never reached?

Also:
Could we add also a test for testing when "no start"/"no length"/"no start and no length" are given?

Note:

+++ b/core/modules/migrate/tests/src/Unit/process/SubstrTest.php
@@ -35,6 +35,7 @@ public function testSubstrSuccess() {
    * @expectedException \Drupal\migrate\MigrateException
+   *
    * @expectedExceptionMessage The input value must be a string.

I'm not sure we want this blank line here... there are not any usages to compare to in core. But IMHO they should be next to each other.
Docs on phpunit puts them without a blank line too: https://phpunit.de/manual/current/en/appendixes.annotations.html#appendi...

penyaskito’s picture

Issue tags: +DevDaysMilan

Tagging for the sprint.

mallezie’s picture

Assigned: Unassigned » mallezie

I'll go further on this one.

mallezie’s picture

Think i added the tests, not sure anymore is needed, let's wait what the bot says.
Removed the unreached assert.

Didn't remove the extra newlines (i've added them since code sniffer complained about them).
I see your reasoning, but perhaps make it a code style change.
Now only thing i can find in our code style is on https://www.drupal.org/node/1354

Separate different-type sections by a blank line (for instance, all the @param documentation goes together, with a blank line before the first parameter and a blank line after the last parameter before the @return section starts)

mallezie’s picture

Assigned: mallezie » Unassigned
Status: Needs work » Needs review
mallezie’s picture

Just a small test against the 8.2 branch

kevinquillen’s picture

Patch works (8.2.x), and upon changing the process plugin to substr here to prevent deduping roles (but still truncating to 32 characters), it also helps resolve #2716133: Roles are duplicated instead of updated existing roles.

mikeryan’s picture

Status: Needs review » Needs work

The plugin itself looks functionally good, thanks. Some tweaks:

  1. +++ b/core/modules/migrate/src/Plugin/migrate/process/Substr.php
    @@ -0,0 +1,41 @@
    + * Gets a substring.
    

    Needs just a bit more, like "This plugin returns a substring of the current value."

  2. +++ b/core/modules/migrate/src/Plugin/migrate/process/Substr.php
    @@ -0,0 +1,41 @@
    +      throw new MigrateException('The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
    

    First "key" should be "value".

  3. +++ b/core/modules/migrate/src/Plugin/migrate/process/Substr.php
    @@ -0,0 +1,41 @@
    +      throw new MigrateException('The character length configuration key should be an integer. Omit this key to capture the entire string.');
    

    Ditto. And, should indicate that omitting the key returns the string from the start point (not the entire string if start > 0).

  4. +++ b/core/modules/migrate/tests/src/Unit/process/SubstrTest.php
    @@ -0,0 +1,103 @@
    +    $configuration['start'] = 0;
    +    $configuration['length'] = 7;
    +    $this->plugin = new Substr($configuration, 'map', []);
    

    I'm not sure there's a point in creating a plugin in setUp() when most test methods create their own.

  5. +++ b/core/modules/migrate/tests/src/Unit/process/SubstrTest.php
    @@ -0,0 +1,103 @@
    +    parent::setUp();
    

    Do not call setUp in a test method.

  6. +++ b/core/modules/migrate/tests/src/Unit/process/SubstrTest.php
    @@ -0,0 +1,103 @@
    +    parent::setUp();
    

    Do not call setUp in a test method.

Also, it'd be good to have a test with both a start > 0 and a length provided.

quietone’s picture

Status: Needs work » Needs review
FileSize
5.65 KB
4.02 KB

Fixed all issues from #12 and removed the blanks lines as per #5.

dawehner’s picture

+++ b/core/modules/migrate/tests/src/Unit/process/SubstrTest.php
@@ -0,0 +1,109 @@
+
+  /**
+   * Tests valid input value.
+   */
+  public function testSubstrSuccess() {
+    $value = $this->plugin->transform('Captain Janeway', $this->migrateExecutable, $this->row, 'destinationproperty');
+    $this->assertSame('Captain', $value);
+  }
+
+  /**
+   * Tests valid input value without given start input value.
+   */
+  public function testSubstrNoStart() {
+    $configuration['length'] = 7;
+    $this->plugin = new Substr($configuration, 'map', []);
+    $value = $this->plugin->transform('Captain Janeway', $this->migrateExecutable, $this->row, 'destinationproperty');
+    $this->assertSame('Captain', $value);
+  }
+
+  /**
+   * Tests valid input value without given length input value.
+   */
+  public function testSubstrNoLength() {
+    $configuration['start'] = 1;
+    $this->plugin = new Substr($configuration, 'map', []);
+    $value = $this->plugin->transform('Captain Janeway', $this->migrateExecutable, $this->row, 'destinationproperty');
+    $this->assertSame('aptain Janeway', $value);
+  }
+
+  /**
+   * Tests valid input value with start > 0 and length.
+   */
+  public function testSubstrWithStartLength() {
+    $configuration['start'] = 6;
+    $configuration['length'] = 3;
+    $this->plugin = new Substr($configuration, 'map', []);
+    $value = $this->plugin->transform('Captain Janeway', $this->migrateExecutable, $this->row, 'destinationproperty');
+    $this->assertSame('n J', $value);
+  }
+
+  /**
+   * Tests valid input value without given start nor length input value.
+   */
+  public function testSubstrNoStartLength() {
+    $configuration = [];
+    $this->plugin = new Substr($configuration, 'map', []);
+    $value = $this->plugin->transform('Captain Janeway', $this->migrateExecutable, $this->row, 'destinationproperty');
+    $this->assertSame('Captain Janeway', $value);
+  }
+

Did you considered to use a @dataProvider here? This could improve the readability of this test quite a bit.

quietone’s picture

@dawehner, thx. I have changed the test to use @dataProvider.

quietone’s picture

Although, the docs for Unicode::substr state that it follows the same behavior as PHP's own substr() function. But substr() return an empty string when length is NULL and Unicode:substr doesn't. Is there a reason for that?

dawehner’s picture

This function is coming directly from drupal_substr in D7 etc. The initial conversion patch: https://www.drupal.org/files/drupal_1938670_00.patch had this behaviour already as well.

I would guess its in order to distinct between no characters (0) and NULL (no cutting).

quietone’s picture

@dawehner, thank you.

mikeryan’s picture

dawehner’s picture

+++ b/core/modules/migrate/tests/src/Unit/process/SubstrTest.php
@@ -0,0 +1,92 @@
+   *
+   * @expectedException \Drupal\migrate\MigrateException
+   * @expectedExceptionMessage The input value must be a string.
+   */
...
+   *
+   * @expectedException \Drupal\migrate\MigrateException
+   * @expectedExceptionMessage The start position configuration value should be an integer. Omit this key to capture from the beginning of the string.
+   */
+  public function testStartIsString() {
...
+   *
+   * @expectedException \Drupal\migrate\MigrateException
+   * @expectedExceptionMessage The character length configuration value should be an integer. Omit this key to capture from the start position to the end of the string.
+   */

For future patches: https://thephp.cc/news/2016/02/questioning-phpunit-best-practices might be interesting to read

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

I actually wanted to RTBC it though.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 05a3cb70fea9ec33581ab8e5c6b7cd60b455503d to 8.2.x and a72245a to 8.1.x. Thanks!

diff --git a/core/modules/migrate/tests/src/Unit/process/SubstrTest.php b/core/modules/migrate/tests/src/Unit/process/SubstrTest.php
index 4e0e06a..fb84509 100644
--- a/core/modules/migrate/tests/src/Unit/process/SubstrTest.php
+++ b/core/modules/migrate/tests/src/Unit/process/SubstrTest.php
@@ -33,24 +33,24 @@ public function testSubstr($start = NULL, $length = NULL, $expected = NULL) {
     $this->assertSame($expected, $value);
   }
 
-    /**
-     * Data provider for testSubstr().
-     */
-    public function providerTestSubstr() {
-      return [
-        // Tests with valid start and length values.
-        [0, 7, 'Captain'],
-        // Tests with valid start > 0 and valid length.
-        [6, 3, 'n J'],
-        // Tests with valid start < 0 and valid length.
-        [-7, 4, 'Jane'],
-        // Tests without start value and valid length value.
-        [NULL, 7, 'Captain'],
-        // Tests with valid start value and no length value.
-        [1, NULL, 'aptain Janeway'],
-        // Tests without both start and length values.
-        [NULL, NULL, 'Captain Janeway'],
-      ];
+  /**
+   * Data provider for testSubstr().
+   */
+  public function providerTestSubstr() {
+    return [
+      // Tests with valid start and length values.
+      [0, 7, 'Captain'],
+      // Tests with valid start > 0 and valid length.
+      [6, 3, 'n J'],
+      // Tests with valid start < 0 and valid length.
+      [-7, 4, 'Jane'],
+      // Tests without start value and valid length value.
+      [NULL, 7, 'Captain'],
+      // Tests with valid start value and no length value.
+      [1, NULL, 'aptain Janeway'],
+      // Tests without both start and length values.
+      [NULL, NULL, 'Captain Janeway'],
+    ];
   }
 
   /**

Fixed indentation on commit.

  • alexpott committed 05a3cb7 on 8.2.x
    Issue #2752591 by quietone, mallezie, dawehner, mikeryan: Add substr...

  • alexpott committed a72245a on 8.1.x
    Issue #2752591 by quietone, mallezie, dawehner, mikeryan: Add substr...

Status: Fixed » Closed (fixed)

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