TypedEntityExampleWebTestCase.php 2.75 KB
Newer Older
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

/**
 * @file
 * Contains \Drupal\typed_entity_example\Tests\TypedEntityExampleWebTestCase
 */

namespace Drupal\typed_entity_example\Tests;

use Drupal\typed_entity\TypedEntity\TypedEntity;
use Drupal\typed_entity\TypedEntity\TypedEntityManager;
use Drupal\typed_entity_example\TypedEntity\Node\Article;
use Drupal\typed_entity_example\TypedEntity\TypedNode;

class TypedEntityExampleWebTestCase extends \DrupalWebTestCase {

  /**
   * Declare test information.
   *
   * @return array
   *   The information array.
   */
  public static function getInfo() {
    return array(
      'name' => 'Typed entity example (functional)',
      'description' => 'Functional tests for Typed Entity.',
      'group' => 'Typed Entity',
    );
  }

  /**
   * Set up.
   */
  protected function setUp() {
    parent::setUp('typed_entity_example');
  }

  /**
   * Test factory.
   */
  public function testFactory() {
    $article = $this->drupalCreateNode(array(
      'type' => 'article',
      'title' => 'Test article',
    ));
    $typed_article = TypedEntityManager::create('node', $article);
    $reflection_article = new \ReflectionClass($typed_article);
    if ($reflection_article->name == 'Drupal\typed_entity_example\TypedEntity\Node\Article') {
      $this->pass('The hook_typed_entity_registry_info is taking precedence.');
    }
    else {
      $this->fail('The hook_typed_entity_registry_info is not taking precedence.');
    }

    // Test the fallback to the TypedNode.
    $page = $this->drupalCreateNode(array(
      'type' => 'page',
      'title' => 'Test article',
    ));
    $typed_page = TypedEntityManager::create('node', $page);

    $reflection_page = new \ReflectionClass($typed_page);
    if ($reflection_page->name == 'Drupal\typed_entity_example\TypedEntity\TypedNode') {
      $this->pass('The factory is falling back to TypedNode.');
    }
    else {
      $this->fail('The factory is not falling back to TypedNode.');
    }

    // Test the fallback to TypedEntity.
    $account = $this->drupalCreateUser();
    $typed_user = TypedEntityManager::create('user', $account);
    $reflection_user = new \ReflectionClass($typed_user);
    if ($reflection_user->name == 'Drupal\typed_entity\TypedEntity\TypedEntity') {
      $this->pass('The factory is falling back to TypedEntity.');
    }
    else {
      $this->fail('The factory is not falling back to TypedEntity.');
    }
  }

Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
82
83
84
85
86
87
88
89
90
91
92
93
94
  /**
   * Test the service container integration.
   */
  public function testServiceContainer() {
    $article = $this->drupalCreateNode(array(
      'type' => 'article',
      'title' => 'Test article',
    ));
    $typed_article = TypedEntityManager::create('node', $article);
    $wrapper = $typed_article->getWrapper();
    $this->assertTrue($wrapper instanceof \EntityDrupalWrapperInterface);
  }

Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
95
}