While our default tracker plugin's functionality is, of course, being implicitly tested in several other tests, there is no specific test case just for that plugin (unless I'm mistaken). This should be amended.

Comments

drunken monkey created an issue. See original summary.

borisson_’s picture

This sounds like a great idea. Should this be a kernel test?

drunken monkey’s picture

Yes, a Kernel test sounds most reasonable here. Without a DB connection, you won't get very far (and mocking that is bordering on the absurd), but there's no reason for going full Functional.

drunken monkey’s picture

Status: Active » Needs review
StatusFileSize
new18.58 KB

OK, here it is, and I actually spotted a problem through this. And not even with the new LIFO option – on the contrary, something that's only a problem with FIFO:

@@ -266,6 +266,12 @@ public function trackItemsUpdated(array $ids = NULL) {
         if ($ids_chunk) {
           $update->condition('item_id', $ids_chunk, 'IN');
         }
+        // Update the status of unindexed items only if the item order is LIFO.
+        // (Otherwise, an item that's regularly being updated might never get
+        // indexed.)
+        if ($this->configuration['indexing_order'] === 'fifo') {
+          $update->condition('status', static::STATUS_INDEXED);
+        }
         $update->execute();
       }
     }

Open question: Should we do the same for trackAllItemsUpdated()? Or is that expected to update all items to the same "changed" timestamp, no matter whether they were indexed before?

A second set of fails I had to fix (since I went a bit berserk and aimed for a 100% coverage) was that, while all the other methods took care to not let an exception escape (since the interface doesn't allow for exceptions), the ones for returning the various counts (and getRemainingItems()) didn't. To be consistent, I added try/catch blocks for those, too.
(The possible exceptions are, in all cases, just database exceptions that should only occur when the database suddenly goes away. Actually, hiding these exceptions and just carrying on seems a bit dangerous, but if the database connection is lost I don't really know what else we could do. Probably, whatever event we wanted to track in such a case would fail anyways.)

borisson_’s picture

+++ b/tests/src/Kernel/BasicTrackerTest.php
@@ -0,0 +1,382 @@
+    // @todo Change to createMock() once we depend on Drupal 8.5+. See #2930839.
+    /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Log\LoggerInterface $logger */
+    $logger = $this->getMock(LoggerInterface::class);
+    $logger->method('log')
+      ->willReturnCallback(function ($level, $message, array $variables) {
+        $error = 'Tracking operation threw ';
+        $error .= strtr($message, $variables);
+        throw new \Exception($error);
+      });
+    $this->tracker->setLogger($logger);

We can replace this with the following, so we don't have to add the @todo.
It also looks like the returncallback isn't used here. At least, when I replaced that locally all tests still passed.

    /** @var \Prophecy\Prophecy\ObjectProphecy|\Psr\Log\LoggerInterface $logger */
    $logger = $this->prophesize(LoggerInterface::class);
    $logger->log(Argument::any(), Argument::any());
    $this->tracker->setLogger($logger->reveal());

This is the only thing I could find.

drunken monkey’s picture

StatusFileSize
new733 bytes
new18.72 KB

Thanks a lot for reviewing!

We can replace this with the following, so we don't have to add the @todo.
It also looks like the returncallback isn't used here. At least, when I replaced that locally all tests still passed.

The callback ensures that a test fails if an exception occurs within a method. So, it's expected that the tests would still pass without this whole block. Maybe we should just add a comment to make that clearer? Patch attached.

I also actually like that we add the @todo, since we don't have one in the code base for that issue yet anyways.

I've never used this "prophecy" thing. Seems like modern nonsense. ;P Is it really better than a normal mock, or just different syntax?

So, is this RTBC with the addition of the comment?

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Prophecy is another mocking framework that is shipped with phpunit, I think the facets unit tests are about 50/50 phpunit/prophecy. Its clear now. Great work!

  • drunken monkey committed 657fa3c on 8.x-1.x
    Issue #2928279 by drunken monkey, borisson_: Added a test for the...
drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Great to hear, thanks a lot again for reviewing!
Committed.

  • drunken monkey committed 48c2e10 on 8.x-1.x
    Follow-up to #2928279 by drunken monkey: Removed unused use statement.
    

Status: Fixed » Closed (fixed)

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