commit 9d62be137f7bd047a4e2265dfe4439e7461c92e7
Author: naught101 <naught101@44216.no-reply.drupal.org>
Date:   Sat May 4 23:47:56 2013 +1000

    relation_create/save validation tests

diff --git a/tests/relation.test b/tests/relation.test
index ca9d008..1c11d71 100644
--- a/tests/relation.test
+++ b/tests/relation.test
@@ -138,9 +138,27 @@ class RelationTestCase extends DrupalWebTestCase {
       'max_arity' => 1,
       'source_bundles' => array('node:page'),
     );
+    $this->relation_types['relation_requires_field'] = array(
+      'relation_type' => 'relation_requires_field',
+      'label' => 'relation_requires_field',
+      'source_bundles' => array('node:*'),
+    );
     foreach ($this->relation_types as $relation_type) {
       relation_type_save($relation_type);
     }
+
+    // attach a field to the relation_requires_field relation type;
+    field_create_field(array(
+      'field_name' => 'field_required_integer',
+      'type' => 'number_integer'
+    ));
+    field_create_instance(array(
+      'label' => 'Required Integer Field',
+      'required' => TRUE,
+      'field_name' => 'field_required_integer',
+      'entity_type' => 'relation',
+      'bundle' => 'relation_requires_field',
+    ));
   }
 
   /**
@@ -404,6 +422,45 @@ class RelationAPITestCase extends RelationTestCase {
           ->fetchAllAssoc('rid');
       $this->assertTrue(array_key_exists($rid, $revision), 'Relation revision created.');
     }
+
+    // Relation save should fail if the relation type doesn't exist
+    try {
+      $rid = $this->saveRelation('non_existant_relation_type', $this->endpoints_entitydifferent);
+    }
+    catch (Exception $e) {
+      $this->assertEqual("Relation save failed: Relation type $relation_type does not exist.", $e->getMessage, 'Prevented creation of a relation with a non-existant relation type.');
+    }
+
+    // Relation save should fail if relation endpoints are invalid.
+    try {
+      $rid = $this->saveRelation('directional_entitysame', $this->endpoints_entitydifferent);
+    }
+    catch (Exception $e) {
+      $this->assertEqual("Relation save failed: Relation type directional_entitysame does allow entities of type user:user as a source endpoint.", $e->getMessage, 'Prevented creation of a relation with the wrong endpoint types.');
+    }
+
+    // Relation save should fail if there is no data present in a field which is
+    // required on the relation type.
+    // This should probably be handled by Field API, no? What will the error
+    // message be?
+    try {
+      $rid = $this->saveRelation('relation_requires_field', $this->endpoints_entitysame);
+    }
+    catch (Exception $e) {
+      $this->assertEqual("Relation save failed: There is no data in the field_required_integer field.", $e->getMessage, 'Prevented creation of a relation with missing data in a required field.');
+    }
+
+    // Relation save should fail if there is invalid data present in a field.
+    // This should probably be handled by Field API, no? What will the error
+    // message be?
+    try {
+      $relation = relation_create('relation_requires_field', $this->endpoints_entitysame);
+      $relation->field_required_integer[LANGUAGE_NONE][0]['value'] = 'This should cause an error';
+      $rid = relation_save($relation);
+    }
+    catch (Exception $e) {
+      $this->assertEqual("Relation save failed: invalid values provided for field_required_integer.", $e->getMessage, 'Prevented creation of a relation with invalid data in a field.');
+    }
   }
 
   /**
