From 00b12b3ea96b9d4f352b49f45e20b48fc2b38245 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Sat, 9 Feb 2013 03:41:24 -0800
Subject: [PATCH] Issue #1803586 by linclark: Added Give all entities their own
 URI.

---
 core/lib/Drupal/Core/Entity/Entity.php        | 21 ++++---
 .../Drupal/Core/Entity/EntityInterface.php    |  3 +-
 .../system/Tests/Entity/EntityUriTest.php     | 55 +++++++++++++++++++
 3 files changed, 68 insertions(+), 11 deletions(-)
 create mode 100644 core/modules/system/lib/Drupal/system/Tests/Entity/EntityUriTest.php

diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php
index b8feea4c5642..fe00ef3d92a1 100644
--- a/core/lib/Drupal/Core/Entity/Entity.php
+++ b/core/lib/Drupal/Core/Entity/Entity.php
@@ -161,19 +161,22 @@ public function uri() {
     elseif (isset($entity_info['uri_callback'])) {
       $uri_callback = $entity_info['uri_callback'];
     }
-    else {
-      return NULL;
-    }
 
-    // Invoke the callback to get the URI. If there is no callback, return NULL.
+    // Invoke the callback to get the URI. If there is no callback, use the
+    // default URI format.
     if (isset($uri_callback) && function_exists($uri_callback)) {
       $uri = $uri_callback($this);
-      // Pass the entity data to url() so that alter functions do not need to
-      // look up this entity again.
-      $uri['options']['entity_type'] = $this->entityType;
-      $uri['options']['entity'] = $this;
-      return $uri;
     }
+    else {
+      $uri = array(
+        'path' => 'entity/' . $this->entityType . '/' . $this->id(),
+      );
+    }
+    // Pass the entity data to url() so that alter functions do not need to
+    // look up this entity again.
+    $uri['options']['entity_type'] = $this->entityType;
+    $uri['options']['entity'] = $this;
+    return $uri;
   }
 
   /**
diff --git a/core/lib/Drupal/Core/Entity/EntityInterface.php b/core/lib/Drupal/Core/Entity/EntityInterface.php
index 40b9f71723fc..34038e9e269f 100644
--- a/core/lib/Drupal/Core/Entity/EntityInterface.php
+++ b/core/lib/Drupal/Core/Entity/EntityInterface.php
@@ -132,8 +132,7 @@ public function label($langcode = NULL);
    *
    * @return
    *   An array containing the 'path' and 'options' keys used to build the URI
-   *   of the entity, and matching the signature of url(). NULL if the entity
-   *   has no URI of its own.
+   *   of the entity, and matching the signature of url().
    */
   public function uri();
 
diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUriTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUriTest.php
new file mode 100644
index 000000000000..66a00e54553e
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUriTest.php
@@ -0,0 +1,55 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\system\Tests\Entity\EntityUriTest.
+ */
+
+namespace Drupal\system\Tests\Entity;
+
+use Drupal\simpletest\DrupalUnitTestBase;
+
+/**
+ * Tests the basic Entity API.
+ */
+class EntityUriTest extends DrupalUnitTestBase {
+
+  /**
+   * Modules to load.
+   *
+   * @var array
+   */
+  public static $modules = array('field', 'field_sql_storage', 'system', 'text');
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Entity URI',
+      'description' => 'Tests default URI functionality.',
+      'group' => 'Entity API',
+    );
+  }
+
+  protected function setUp() {
+    parent::setUp();
+
+    $this->installSchema('system', 'variable');
+    $this->installSchema('system', 'url_alias');
+    $this->installSchema('field', 'field_config');
+    $this->installSchema('field', 'field_config_instance');
+
+    $this->enableModules(array('entity_test'));
+  }
+
+  /**
+   * Tests that an entity without a URI callback uses the default URI.
+   */
+  function testDefaultUri() {
+    // Create a test entity.
+    $entity = entity_create('entity_test', array('name' => 'test', 'user_id' => 1));
+    $entity->save();
+    $uri = $entity->uri();
+    $expected_path = 'entity/entity_test/' . $entity->id();
+    $this->assertEqual(url($uri['path'], $uri['options']), url($expected_path), 'Entity without URI callback returns expected URI.');
+  }
+
+}
-- 
GitLab