Default comments from anonymous break in comment statistics
Not sure yet what needs to be fixed where, but something between now and Dec 14 broke anonymous comments.
It is failing with a comment_last_uid can not be NULL exception from comment storage, so the easiest quick fix is to cast it to (int) there. But I don't think that's correct.
I'm however not sure how it is supposed to export it. The problem is that there is no way that we can control the UUID of user 0 in a new installation. so the UUID reference can obviously not resolve that. Maybe the serializer should fallback to just store 0 as the value then? I know we worked on an issue to allow 0 as a valid return value from the resolve ID serializer stuff... here: https://www.drupal.org/node/2327935
But that alone will not be enough to fix this, just should make the import work.
CommentViewBuilder and possibly other places also fail when the uid is NULL.
Ok. https://www.drupal.org/node/2327935 got in and I tried to write a test to verify this but I think my situation is special.
When writing a test, either the user 0 does not exist (in a kernel test), then it writes out target_id => 0, because it can't load and falls back to the parent normalizer. And with the fix from that issue, we also load it again.
Or uid 0 exists, it adds the reference, and can also normalize it again because the UUID is the same.
What's special in my case is that is default content in an install profile and obviously, uid 0 and 1 always have a different UUID. so I can't reliably reference it. but I can manually change it to a simple target_id => 0, and that will work for me. I'm not sure if that is possible a generic way. I'm ok with closing this.
Here's the test that I wrote, added to EntityTest::testComment(). Works with and without the uid 0 creation part.
<?php
// Insert a row for the anonymous user, to mimick a real site. Otherwise
// the normalizer fails to load the entity and will fall back to the
$storage = \Drupal::entityManager()->getStorage('user');
$storage
->create(array(
'uid' => 0,
'status' => 0,
))
->save();
$anon_comment = entity_create('comment', array(
'uid' => 0,
'name' => 'Anonymous user',
'subject' => $this->randomMachineName(),
'comment_body' => [
'value' => $this->randomMachineName(),
'format' => NULL,
],
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'pid' => $parent_comment->id(),
'mail' => '[email protected]',
'homepage' => 'http://buytaert.net',
));
$anon_comment->save();
$original_values = $anon_comment->toArray();
debug($original_values);
// cid will not exist and hostname will always be denied view access.
unset($original_values['cid'], $original_values['hostname']);
$normalized = $this->serializer->normalize($anon_comment, $this->format, ['account' => $account]);
debug($normalized);
// Assert that the hostname field does not appear at all in the normalized
// data.
$this->assertFalse(array_key_exists('hostname', $normalized), 'Hostname was not found in normalized comment data.');
$denormalized_anon_comment = $this->serializer->denormalize($normalized, 'Drupal\comment\Entity\Comment', $this->format, ['account' => $account]);
// Verify that the ID and revision ID were skipped by the normalizer.
$this->assertEqual(NULL, $denormalized_anon_comment->id());
// Loop over the remaining fields and verify that they are identical.
foreach ($original_values as $field_name => $field_values) {
$this->assertEqual($field_values, $denormalized_anon_comment->get($field_name)->getValue());