danielebarchiesi@4: 'Node reference', danielebarchiesi@4: 'description' => 'Make sure nodes are referenceable in entity forms.', danielebarchiesi@4: 'group' => 'References', danielebarchiesi@4: ); danielebarchiesi@4: } danielebarchiesi@4: danielebarchiesi@4: function setUp() { danielebarchiesi@4: parent::setUp(array('node_reference', 'field_test')); danielebarchiesi@4: danielebarchiesi@4: $this->langcode = LANGUAGE_NONE; danielebarchiesi@4: $this->field_name = 'test_node_reference'; danielebarchiesi@4: $this->field = array( danielebarchiesi@4: 'field_name' => $this->field_name, danielebarchiesi@4: 'type' => 'node_reference', danielebarchiesi@4: 'cardinality' => 1, danielebarchiesi@4: 'settings' => array( danielebarchiesi@4: 'referenceable_types' => array_keys(node_type_get_names()), danielebarchiesi@4: ), danielebarchiesi@4: ); danielebarchiesi@4: $this->field = field_create_field($this->field); danielebarchiesi@4: $this->instance = array( danielebarchiesi@4: 'field_name' => $this->field_name, danielebarchiesi@4: 'entity_type' => 'test_entity', danielebarchiesi@4: 'bundle' => 'test_bundle', danielebarchiesi@4: 'widget' => array( danielebarchiesi@4: 'type' => 'options_buttons', danielebarchiesi@4: ), danielebarchiesi@4: ); danielebarchiesi@4: danielebarchiesi@4: $this->instance = field_create_instance($this->instance); danielebarchiesi@4: danielebarchiesi@4: $this->nodes = array(); danielebarchiesi@4: foreach (node_type_get_names() as $type_name => $type_title) { danielebarchiesi@4: $this->nodes[$type_name] = $this->drupalCreateNode(array( danielebarchiesi@4: 'type' => $type_name, danielebarchiesi@4: 'title' => $this->randomName(8), danielebarchiesi@4: )); danielebarchiesi@4: $this->pass(t('Created %type node %nid: %title', array( danielebarchiesi@4: '%type' => $type_name, danielebarchiesi@4: '%nid' => $this->nodes[$type_name]->nid, danielebarchiesi@4: '%title' => $this->nodes[$type_name]->title, danielebarchiesi@4: )), 'destination creation'); danielebarchiesi@4: } danielebarchiesi@4: } danielebarchiesi@4: danielebarchiesi@4: function runReferenceableNodeTest($allowed, $group) { danielebarchiesi@4: field_update_field(array( danielebarchiesi@4: 'field_name' => $this->field_name, danielebarchiesi@4: 'settings' => array('referenceable_types' => array_keys($allowed)), danielebarchiesi@4: )); danielebarchiesi@4: $entity = field_test_create_stub_entity(); danielebarchiesi@4: $form = drupal_get_form('field_test_entity_form', $entity); danielebarchiesi@4: $options = $form[$this->field_name][$this->langcode]['#options']; danielebarchiesi@4: $this->assertTrue(isset($options['_none']), t('Empty choice offered for reference'), $group); danielebarchiesi@4: unset($options['_none']); danielebarchiesi@4: foreach ($this->nodes as $node) { danielebarchiesi@4: if (isset($allowed[$node->type])) { danielebarchiesi@4: $this->assertTrue(isset($options[$node->nid]), danielebarchiesi@4: t('Node of type @type is referenceable', array('@type' => $node->type)), danielebarchiesi@4: $group); danielebarchiesi@4: } danielebarchiesi@4: else { danielebarchiesi@4: $this->assertFalse(isset($options[$node->nid]), danielebarchiesi@4: t('Node of type @type is not referenceable', array('@type' => $node->type)), danielebarchiesi@4: $group); danielebarchiesi@4: } danielebarchiesi@4: unset($options[$node->nid]); danielebarchiesi@4: } danielebarchiesi@4: $this->assertTrue(empty($options), t('No extra choice is referenceable'), $group); danielebarchiesi@4: } danielebarchiesi@4: danielebarchiesi@4: /** danielebarchiesi@4: * Test unlimited referencing danielebarchiesi@4: */ danielebarchiesi@4: function testReferenceableNodeTypesAll() { danielebarchiesi@4: $allowed = node_type_get_names(); danielebarchiesi@4: $this->runReferenceableNodeTest($allowed, t('Unimited referencing')); danielebarchiesi@4: } danielebarchiesi@4: danielebarchiesi@4: /** danielebarchiesi@4: * Test referencing a limited list of node types danielebarchiesi@4: */ danielebarchiesi@4: function testReferenceableNodeTypesOne() { danielebarchiesi@4: $allowed = array_slice(node_type_get_names(), 0, 1, TRUE); danielebarchiesi@4: $this->runReferenceableNodeTest($allowed, t('Limited referencing')); danielebarchiesi@4: } danielebarchiesi@4: danielebarchiesi@4: danielebarchiesi@4: /** danielebarchiesi@4: * Test autocomplete widget. danielebarchiesi@4: */ danielebarchiesi@4: function testLongNodeReferenceWidget() { danielebarchiesi@4: // Create regular test user. danielebarchiesi@4: $web_user = $this->drupalCreateUser(array('create article content', 'access content')); danielebarchiesi@4: $this->drupalLogin($web_user); danielebarchiesi@4: danielebarchiesi@4: // Create test field instance on article node type. danielebarchiesi@4: $instance = array( danielebarchiesi@4: 'field_name' => $this->field_name, danielebarchiesi@4: 'entity_type' => 'node', danielebarchiesi@4: 'bundle' => 'article', danielebarchiesi@4: 'widget' => array( danielebarchiesi@4: 'type' => 'node_reference_autocomplete', danielebarchiesi@4: ), danielebarchiesi@4: ); danielebarchiesi@4: $instance = field_create_instance($instance); danielebarchiesi@4: danielebarchiesi@4: // Create a node with a short title and a node with a title longer than danielebarchiesi@4: // 128 characters. danielebarchiesi@4: $node_short_title = $this->drupalCreateNode(array( danielebarchiesi@4: 'type' => 'page', danielebarchiesi@4: 'title' => $this->randomName(8), danielebarchiesi@4: )); danielebarchiesi@4: $node_long_title = $this->drupalCreateNode(array( danielebarchiesi@4: 'type' => 'page', danielebarchiesi@4: 'title' => $this->randomName(200), danielebarchiesi@4: )); danielebarchiesi@4: danielebarchiesi@4: // Display node creation form. danielebarchiesi@4: $langcode = LANGUAGE_NONE; danielebarchiesi@4: $this->drupalGet('node/add/article'); danielebarchiesi@4: $this->assertFieldByName("{$this->field_name}[$langcode][0][nid]", '', t('Widget is displayed')); danielebarchiesi@4: danielebarchiesi@4: // Submit node form with autocomplete value for short title. danielebarchiesi@4: $edit = array( danielebarchiesi@4: 'title' => $this->randomName(8), danielebarchiesi@4: "{$this->field_name}[$langcode][0][nid]" => $node_short_title->title . ' [nid:' . $node_short_title->nid . ']', danielebarchiesi@4: ); danielebarchiesi@4: $this->drupalPost('node/add/article', $edit, t('Save')); danielebarchiesi@4: $this->assertRaw(t('!post %title has been created.', array('!post' => 'Article', '%title' => $edit["title"])), t('Article created.')); danielebarchiesi@4: $this->assertText($node_short_title->title, t('Referenced node title is displayed.')); danielebarchiesi@4: danielebarchiesi@4: // Submit node form with autocomplete value for long title. danielebarchiesi@4: $edit = array( danielebarchiesi@4: 'title' => $this->randomName(8), danielebarchiesi@4: "{$this->field_name}[$langcode][0][nid]" => $node_long_title->title . ' [nid:' . $node_long_title->nid . ']', danielebarchiesi@4: ); danielebarchiesi@4: $this->drupalPost('node/add/article', $edit, t('Save')); danielebarchiesi@4: $this->assertRaw(t('!post %title has been created.', array('!post' => 'Article', '%title' => $edit["title"])), t('Article created.')); danielebarchiesi@4: $this->assertText($node_long_title->title, t('Referenced node title is displayed.')); danielebarchiesi@4: } danielebarchiesi@4: }