Skip to content

Commit

Permalink
Merge pull request FreePBX#3 in FREEPBX/superfecta from feature/FREEP…
Browse files Browse the repository at this point in the history
…BX-12238 to release/13.0

* commit '13d318623a69cca2887f70f656f0c2944461ad08':
  FREEPBX-12238 Add Contact Manager source with test
  • Loading branch information
tm1000 committed Oct 12, 2016
2 parents 8a70bea + 13d3186 commit 7aab9c2
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
61 changes: 61 additions & 0 deletions sources/source-FreePBX_Contactmanager.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Uses FreePBX Contact manager to lookup numbers;
*/
class FreePBX_Contactmanager extends superfecta_base {

public $description;
public $version_requirement = "2.11";
public $source_param = array(
'Return_Format' => array(
'description' => 'The format to return any found caller ID in.',
'type' => 'select',
'option' => array (
'1' => 'Display Name',
'2' => 'Company',
'3' => 'Last First',
'4' => 'First Last',
),
'default' => '4',
),
);

public function __construct() {
$this->description = _("Searches FreePBX Contact manager for name.");
}

function get_caller_id($thenumber, $run_param=array()) {
$caller_id = null;
$data = FreePBX::Contactmanager()->lookupByUserID(-1, $thenumber,"/\D/");
if(empty($data)){
return null;
}
$first = !empty($data['fname'])?$data['fname']:'';
$last = !empty($data['lname'])?$data['lname']:'';
$company = !empty($data['company'])?$data['company']:'Unknown';
$displayname = !empty($data['displayname'])?$data['displayname']:'Unknown';

if(empty($first) && empty($last)){
$first = 'Unknown';
}
$format = isset($run_param['Return_Format'])?$run_param['Return_Format']:'4';
switch($format){
case '1':
$caller_id = $displayname;
break;
case '2':
$caller_id = $company;
break;
case '3':
$caller_id = $last .' ' .$first;
break;
case '4':
default:
$caller_id = $first .' ' .$last;
break;
}

return $caller_id;
}

}
34 changes: 34 additions & 0 deletions utests/source-FreePBX_ContactmanagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* https://blogs.kent.ac.uk/webdev/2011/07/14/phpunit-and-unserialized-pdo-instances/
* @backupGlobals disabled
*/

class FreePBX_ContactmanagerTest extends PHPUnit_Framework_TestCase{
protected static $o;
public static function setUpBeforeClass() {
include 'setuptests.php';
$webroot = FreePBX::Config()->get('AMPWEBROOT');
include $webroot.'/admin/modules/superfecta/includes/superfecta_base.php';
include $webroot.'/admin/modules/superfecta/sources/source-FreePBX_Contactmanager.module';
self::$o = new FreePBX_Contactmanager();
}
//Stuff before the test
public function setup() {}
//Leave this alone, it test that PHPUnit is working
public function testPHPUnit() {
$this->assertEquals("test", "test", "PHPUnit is broken.");
$this->assertNotEquals("test", "nottest", "PHPUnit is broken.");
}

//This tests that the the object for your class is an object
public function testCreate() {;
$this->assertTrue(is_object(self::$o), "Did not get an object");
}

public function testCnam(){
$cnam = self::$o->get_caller_id('4805551212',array('Return Format' => 3));
$this->assertEquals("Jimmy John", $cnam, "The lookup returned an unexpected result for 6305434316");
}
}

0 comments on commit 7aab9c2

Please sign in to comment.