Example #1
0
 /**
  * Links (ie: URLs) can be associated with fields of a table by defining a "link"
  * property for a field in the fields.ini file or by defining a fieldname__link()
  * method in the delegate class.  These links are accessible via the Table::getLink()
  * method which resolves any variables in the link provided and returns the link
  * in the same format as it is defined in its place of origin (either the fields.ini file
  * or the delegate class.
  *
  * Links can be in the form of a query array, or in the form of a string (which should
  * be a url).
  * In this test, `fname`, `lname`, and `description` fields have links defined in the 
  * Profiles delegate class.  
  */
 function test_links()
 {
     $profiles = new Dataface_Record('Profiles', array());
     // Part I:  Testing links produced by the delegate *__link() methods.
     //--------------------------------------------------------------------
     // First test a link with explicit values where the delegate method __link() is defined
     // and the delegate method explicitly returns an array.
     $profiles->setValues(array("fname" => "John", "lname" => "Thomas"));
     $link = $profiles->getLink("fname");
     $this->assertEquals($link["fname"], "John");
     $this->assertEquals($link["lname"], "Thomas");
     $this->assertEquals($link["description"], "My name is John");
     // Try the same thing where no link is defined
     $link = $profiles->getLink("id");
     $this->assertEquals($link, null);
     // Now test a link with implicit values where the delegate method __link() is defined
     // and the delegate method returns an array.
     $table = new Dataface_Record('Profiles', array());
     $table->clearValues();
     $table->setValues(array("fname" => "John", "lname" => "Thomas"));
     $link = $profiles->getLink("fname");
     $this->assertEquals($link["fname"], "John");
     $this->assertEquals($link["lname"], "Thomas");
     $this->assertEquals($link["description"], "My name is John");
     // Try same thing when no link is defined
     $link = $profiles->getLink("id");
     $this->assertEquals($link, null);
     //Now test the link returned by a field that returns the link as a string with no
     // variables that need parsing.
     $link = $profiles->getLink("lname");
     $this->assertEquals("http://www.google.ca?fname=John&lname=Thomas", $link);
     // Now test the link returned by a field that returns a link that contains
     // variables (which should automatically be resolved by the getLink() method.
     // This tests the delegate still.
     $link = $profiles->getLink("description");
     $this->assertEquals("http://www.google.ca?fname=John&lname=Thomas", $link);
     // Part II: Testing links defined in the fields.ini file
     //-------------------------------------------------------------------------
     // Test a link with no variables requiring resolving
     $link = $profiles->getLink("dob");
     $this->assertEquals("http://www.google.ca", $link);
     // Test a link with variables that need to be resolved.
     $link = $profiles->getLink("phone1");
     $this->assertEquals("http://www.google.ca?fname=John", $link);
     $profiles->clearValues();
     $profiles->setValue("fname", "Thomas");
     $link = $profiles->getLink("phone1");
     $this->assertEquals("http://www.google.ca?fname=Thomas", $link);
 }