commit 7660c112f68a603a4de8b07bfaf98d2e7017808f
Author: Ilias Dimopoulos <idimopoulos@hotmail.com>
Date:   Tue Apr 12 15:04:39 2016 +0200

    Provide test for string ID.

diff --git a/tests/src/Kernel/EntityStringIdTest.php b/tests/src/Kernel/EntityStringIdTest.php
new file mode 100644
index 0000000..4aff038
--- /dev/null
+++ b/tests/src/Kernel/EntityStringIdTest.php
@@ -0,0 +1,198 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\search_api\Kernel\EntityStringIdTest.
+ */
+
+namespace Drupal\Tests\search_api\Kernel;
+
+use Drupal\entity_test\Entity\EntityTestStringId;
+use Drupal\KernelTests\KernelTestBase;
+use Drupal\search_api\Entity\Index;
+use Drupal\search_api\Entity\Server;
+
+/**
+ * Tests entity indexing that are using string IDs.
+ *
+ * Current limit of the search_api id is 50 characters. The format of the
+ * saved item is entity:<entity_type_id>:<entity_id>:<language_code>.
+ * For this test we are using:
+ *
+ * entity:entity_test_string_id:<string_id>:und
+ * entity = 6 characters.
+ * entity_test_string_id = 21 characters.
+ * und = 3 characters.
+ * 3 x ':' = 3 characters.
+ * 50 - 6 - 21 - 3 - 3 = 17 characters left for the ID.
+ *
+ * @group search_api
+ */
+class EntityStringIdTest extends KernelTestBase {
+
+  /**
+   * The test entity type used in the test.
+   *
+   * @var string
+   */
+  protected $testEntityTypeId = 'entity_test_string_id';
+
+  /**
+   * The search server used for testing.
+   *
+   * @var \Drupal\search_api\ServerInterface
+   */
+  protected $server;
+
+  /**
+   * The search index used for testing.
+   *
+   * @var \Drupal\search_api\IndexInterface
+   */
+  protected $index;
+
+  /**
+   * Modules to enable for this test.
+   *
+   * @var string[]
+   */
+  public static $modules = array(
+    'search_api',
+    'search_api_test_backend',
+    'language',
+    'user',
+    'system',
+    'entity_test',
+  );
+
+  /**
+   * An array of langcodes.
+   *
+   * @var string[]
+   */
+  protected $langcodes;
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setUp() {
+    parent::setUp();
+
+    // Enable translation for the entity_test module.
+    \Drupal::state()->set('entity_test_string_id.translation', FALSE);
+
+    $this->installSchema('search_api', array('search_api_item', 'search_api_task'));
+    $this->installEntitySchema('entity_test_string_id');
+
+    // Do not use a batch for tracking the initial items after creating an
+    // index when running the tests via the GUI. Otherwise, it seems Drupal's
+    // Batch API gets confused and the test fails.
+    \Drupal::state()->set('search_api_use_tracking_batch', FALSE);
+
+    // Create a test server.
+    $this->server = Server::create(array(
+      'name' => 'Test Server',
+      'id' => 'test_server',
+      'status' => 1,
+      'backend' => 'search_api_test_backend',
+    ));
+    $this->server->save();
+
+    // Create a test index.
+    $this->index = Index::create(array(
+      'name' => 'Test Index',
+      'id' => 'test_index',
+      'status' => 1,
+      'datasource_settings' => array(
+        'entity:' . $this->testEntityTypeId => array(
+          'plugin_id' => 'entity:' . $this->testEntityTypeId,
+          'settings' => array(),
+        ),
+      ),
+      'tracker_settings' => array(
+        'default' => array(
+          'plugin_id' => 'default',
+          'settings' => array(),
+        )
+      ),
+      'server' => $this->server->id(),
+      'options' => array('index_directly' => FALSE),
+    ));
+    $this->index->save();
+  }
+
+  /**
+   * Tests plain string Ids.
+   *
+   * Plain string id is an id that contains only letters, numbers and the '_'
+   * symbol. These characters are matched by regex with the character set
+   * represented by \w.
+   */
+  public function testPlainStringId() {
+    /** @var \Drupal\entity_test\Entity\EntityTestStringId $entity_1 */
+    $entity_1 = EntityTestStringId::create(array(
+      'id' => 'short_string_id',
+      'name' => 'String Test',
+      'user_id' => $this->container->get('current_user')->id(),
+    ));
+    $entity_1->save();
+    // Test that the datasource returns the correct item IDs.
+    $datasource = $this->index->getDatasource('entity:' . $this->testEntityTypeId);
+    $datasource_item_ids = $datasource->getItemIds();
+    sort($datasource_item_ids);
+    // 'short_string_id' = 15 characters.
+    $expected = array(
+      'short_string_id:und'
+    );
+    $this->assertEquals($expected, $datasource_item_ids, 'Datasource returns correct item ids.');
+
+    // Test indexing the new entity.
+    $this->assertEquals(0, $this->index->getTrackerInstance()->getIndexedItemsCount(), 'The index is empty.');
+    $this->assertEquals(1, $this->index->getTrackerInstance()->getTotalItemsCount(), 'There is one item to be indexed.');
+    $this->index->indexItems();
+    $this->assertEquals(1, $this->index->getTrackerInstance()->getIndexedItemsCount(), 'One item has been indexed.');
+  }
+
+  /**
+   * Tests Uris as Ids.
+   *
+   * We are testing a uri because it is not matched by the regex character set
+   * represented by \w because they contain the characters ':' and '/'
+   * which are used to split the string saved in the index
+   * and should not affect it.
+   */
+  public function testUriStringId() {
+    // 'http://drupal.org' = 17 characters.
+    $entity_1 = EntityTestStringId::create(array(
+      'id' => 'http://drupal.org',
+      'name' => 'URI Test',
+      'user_id' => $this->container->get('current_user')->id(),
+    ));
+    $entity_1->save();
+
+    // 'http://drupal.org/project/search_api' = 35 characters.
+    // It should fail indexing.
+    $entity_2 = EntityTestStringId::create(array(
+      'id' => 'http://drupal.org/project/search_api',
+      'name' => 'URI Test 2',
+      'user_id' => $this->container->get('current_user')->id(),
+    ));
+    $entity_2->save();
+
+    // Test that the datasource returns the correct item IDs.
+    $datasource = $this->index->getDatasource('entity:' . $this->testEntityTypeId);
+    $datasource_item_ids = $datasource->getItemIds();
+    sort($datasource_item_ids);
+    $expected = array(
+      'http://drupal.org/project/search_api:und',
+      'http://drupal.org:und',
+    );
+    $this->assertEquals($expected, $datasource_item_ids, 'Datasource returns correct item ids.');
+
+    // Test indexing the new entities. One should fail so only one should be indexed.
+    $this->assertEquals(0, $this->index->getTrackerInstance()->getIndexedItemsCount(), 'The index is empty.');
+    $this->assertEquals(1, $this->index->getTrackerInstance()->getTotalItemsCount(), 'There is one item to be indexed.');
+    $this->index->indexItems();
+    $this->assertEquals(1, $this->index->getTrackerInstance()->getIndexedItemsCount(), 'One item has been indexed.');
+  }
+}
