From 5343e6d0abc7e8601e0b57270bf9ef3b428cebc7 Mon Sep 17 00:00:00 2001
From: acbramley <zombienaute@1036766.no-reply.drupal.org>
Date: Fri, 24 Jun 2022 04:39:48 +0000
Subject: [PATCH] Issue #3292366 by acbramley: Add JSON_PRETTY_PRINT flag

---
 json_ld_schema.module                     |  4 ++--
 src/Element/JsonLdSource.php              |  3 ++-
 src/JsonLdSchemaUtil.php                  | 25 +++++++++++++++++++++++
 tests/src/Functional/JsonLdSourceTest.php | 17 ++++++++++++---
 4 files changed, 43 insertions(+), 6 deletions(-)
 create mode 100644 src/JsonLdSchemaUtil.php

diff --git a/json_ld_schema.module b/json_ld_schema.module
index d23f7c0..1673d66 100644
--- a/json_ld_schema.module
+++ b/json_ld_schema.module
@@ -8,6 +8,7 @@
 use Drupal\Core\Cache\CacheableMetadata;
 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
 use Drupal\Core\Entity\EntityInterface;
+use Drupal\json_ld_schema\JsonLdSchemaUtil;
 
 /**
  * Implements hook_page_bottom().
@@ -62,8 +63,7 @@ function json_ld_schema_entity_view(array &$build, EntityInterface $entity, Enti
       [
         '#tag' => 'script',
         '#attributes' => ['type' => 'application/ld+json'],
-        '#value' => json_encode($json_ld_entity->getData($entity, $view_mode)
-          ->toArray(), JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT),
+        '#value' => JsonLdSchemaUtil::encodeJsonLdData($json_ld_entity->getData($entity, $view_mode)->toArray()),
       ],
       sprintf('json_ld_%s_%s_%s_%s', $entity->getEntityTypeId(), $entity->id(), $view_mode, $definition['id']),
     ];
diff --git a/src/Element/JsonLdSource.php b/src/Element/JsonLdSource.php
index c50d4ff..cc7b0f2 100644
--- a/src/Element/JsonLdSource.php
+++ b/src/Element/JsonLdSource.php
@@ -4,6 +4,7 @@ namespace Drupal\json_ld_schema\Element;
 
 use Drupal\Core\Render\Element\HtmlTag;
 use Drupal\Core\Render\Element\RenderElement;
+use Drupal\json_ld_schema\JsonLdSchemaUtil;
 
 /**
  * A JSON LD source element.
@@ -35,7 +36,7 @@ class JsonLdSource extends RenderElement {
     // that it is cached by render cache.
     $source_manager = \Drupal::service('plugin.manager.json_ld_schema.source');
     $plugin = $source_manager->createInstance($element['#plugin_id']);
-    $element['#value'] = json_encode($plugin->getData()->toArray(), JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
+    $element['#value'] = JsonLdSchemaUtil::encodeJsonLdData($plugin->getData()->toArray());
     return $element;
   }
 
diff --git a/src/JsonLdSchemaUtil.php b/src/JsonLdSchemaUtil.php
new file mode 100644
index 0000000..862cd40
--- /dev/null
+++ b/src/JsonLdSchemaUtil.php
@@ -0,0 +1,25 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Drupal\json_ld_schema;
+
+/**
+ * Helper functions for the module.
+ */
+class JsonLdSchemaUtil {
+
+  /**
+   * Encode JSON:LD data.
+   *
+   * @param array $data
+   *   An array of data to encode.
+   *
+   * @return string
+   *   The encoded string.
+   */
+  public static function encodeJsonLdData(array $data): string {
+    return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_PRETTY_PRINT);
+  }
+
+}
diff --git a/tests/src/Functional/JsonLdSourceTest.php b/tests/src/Functional/JsonLdSourceTest.php
index 81795f9..d4917a6 100644
--- a/tests/src/Functional/JsonLdSourceTest.php
+++ b/tests/src/Functional/JsonLdSourceTest.php
@@ -60,10 +60,10 @@ class JsonLdSourceTest extends BrowserTestBase {
     // count changes.
     $this->setNodeCommentCount(5);
     $this->drupalGet($node->toUrl());
-    $this->assertStringContainsString('"commentCount":5', $this->getSession()->getPage()->getHtml());
+    $this->assertCommentCount(5);
     $this->setNodeCommentCount(10);
     $this->drupalGet($node->toUrl());
-    $this->assertStringContainsString('"commentCount":5', $this->getSession()->getPage()->getHtml());
+    $this->assertCommentCount(5);
 
     // A second node will display the updated comment count.
     $second_node = Node::create([
@@ -72,7 +72,18 @@ class JsonLdSourceTest extends BrowserTestBase {
     ]);
     $second_node->save();
     $this->drupalGet($second_node->toUrl());
-    $this->assertStringContainsString('"commentCount":10', $this->getSession()->getPage()->getHtml());
+    $this->assertCommentCount(10);
+  }
+
+  /**
+   * Assert the comment count that appears on the page.
+   *
+   * @param int $count
+   *   The count.
+   */
+  protected function assertCommentCount(int $count): void {
+    $schema = $this->getSchemaValues('AboutPage');
+    $this->assertEquals($count, $schema['commentCount']);
   }
 
   /**
-- 
GitLab