node_reference.test 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @file
  4. * Initial node_reference tests
  5. */
  6. /**
  7. * Unit tests for referenceability of node types in entity forms.
  8. */
  9. class NodeReferenceFormTest extends FieldTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Node reference',
  13. 'description' => 'Make sure nodes are referenceable in entity forms.',
  14. 'group' => 'References',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp(array('node_reference', 'field_test'));
  19. $this->langcode = LANGUAGE_NONE;
  20. $this->field_name = 'test_node_reference';
  21. $this->field = array(
  22. 'field_name' => $this->field_name,
  23. 'type' => 'node_reference',
  24. 'cardinality' => 1,
  25. 'settings' => array(
  26. 'referenceable_types' => array_keys(node_type_get_names()),
  27. ),
  28. );
  29. $this->field = field_create_field($this->field);
  30. $this->instance = array(
  31. 'field_name' => $this->field_name,
  32. 'entity_type' => 'test_entity',
  33. 'bundle' => 'test_bundle',
  34. 'widget' => array(
  35. 'type' => 'options_buttons',
  36. ),
  37. );
  38. $this->instance = field_create_instance($this->instance);
  39. $this->nodes = array();
  40. foreach (node_type_get_names() as $type_name => $type_title) {
  41. $this->nodes[$type_name] = $this->drupalCreateNode(array(
  42. 'type' => $type_name,
  43. 'title' => $this->randomName(8),
  44. ));
  45. $this->pass(t('Created %type node %nid: %title', array(
  46. '%type' => $type_name,
  47. '%nid' => $this->nodes[$type_name]->nid,
  48. '%title' => $this->nodes[$type_name]->title,
  49. )), 'destination creation');
  50. }
  51. }
  52. function runReferenceableNodeTest($allowed, $group) {
  53. field_update_field(array(
  54. 'field_name' => $this->field_name,
  55. 'settings' => array('referenceable_types' => array_keys($allowed)),
  56. ));
  57. $entity = field_test_create_stub_entity();
  58. $form = drupal_get_form('field_test_entity_form', $entity);
  59. $options = $form[$this->field_name][$this->langcode]['#options'];
  60. $this->assertTrue(isset($options['_none']), t('Empty choice offered for reference'), $group);
  61. unset($options['_none']);
  62. foreach ($this->nodes as $node) {
  63. if (isset($allowed[$node->type])) {
  64. $this->assertTrue(isset($options[$node->nid]),
  65. t('Node of type @type is referenceable', array('@type' => $node->type)),
  66. $group);
  67. }
  68. else {
  69. $this->assertFalse(isset($options[$node->nid]),
  70. t('Node of type @type is not referenceable', array('@type' => $node->type)),
  71. $group);
  72. }
  73. unset($options[$node->nid]);
  74. }
  75. $this->assertTrue(empty($options), t('No extra choice is referenceable'), $group);
  76. }
  77. /**
  78. * Test unlimited referencing
  79. */
  80. function testReferenceableNodeTypesAll() {
  81. $allowed = node_type_get_names();
  82. $this->runReferenceableNodeTest($allowed, t('Unimited referencing'));
  83. }
  84. /**
  85. * Test referencing a limited list of node types
  86. */
  87. function testReferenceableNodeTypesOne() {
  88. $allowed = array_slice(node_type_get_names(), 0, 1, TRUE);
  89. $this->runReferenceableNodeTest($allowed, t('Limited referencing'));
  90. }
  91. /**
  92. * Test autocomplete widget.
  93. */
  94. function testLongNodeReferenceWidget() {
  95. // Create regular test user.
  96. $web_user = $this->drupalCreateUser(array('create article content', 'access content'));
  97. $this->drupalLogin($web_user);
  98. // Create test field instance on article node type.
  99. $instance = array(
  100. 'field_name' => $this->field_name,
  101. 'entity_type' => 'node',
  102. 'bundle' => 'article',
  103. 'widget' => array(
  104. 'type' => 'node_reference_autocomplete',
  105. ),
  106. );
  107. $instance = field_create_instance($instance);
  108. // Create a node with a short title and a node with a title longer than
  109. // 128 characters.
  110. $node_short_title = $this->drupalCreateNode(array(
  111. 'type' => 'page',
  112. 'title' => $this->randomName(8),
  113. ));
  114. $node_long_title = $this->drupalCreateNode(array(
  115. 'type' => 'page',
  116. 'title' => $this->randomName(200),
  117. ));
  118. // Display node creation form.
  119. $langcode = LANGUAGE_NONE;
  120. $this->drupalGet('node/add/article');
  121. $this->assertFieldByName("{$this->field_name}[$langcode][0][nid]", '', t('Widget is displayed'));
  122. // Submit node form with autocomplete value for short title.
  123. $edit = array(
  124. 'title' => $this->randomName(8),
  125. "{$this->field_name}[$langcode][0][nid]" => $node_short_title->title . ' [nid:' . $node_short_title->nid . ']',
  126. );
  127. $this->drupalPost('node/add/article', $edit, t('Save'));
  128. $this->assertRaw(t('!post %title has been created.', array('!post' => 'Article', '%title' => $edit["title"])), t('Article created.'));
  129. $this->assertText($node_short_title->title, t('Referenced node title is displayed.'));
  130. // Submit node form with autocomplete value for long title.
  131. $edit = array(
  132. 'title' => $this->randomName(8),
  133. "{$this->field_name}[$langcode][0][nid]" => $node_long_title->title . ' [nid:' . $node_long_title->nid . ']',
  134. );
  135. $this->drupalPost('node/add/article', $edit, t('Save'));
  136. $this->assertRaw(t('!post %title has been created.', array('!post' => 'Article', '%title' => $edit["title"])), t('Article created.'));
  137. $this->assertText($node_long_title->title, t('Referenced node title is displayed.'));
  138. }
  139. }