diff --git a/lib/classes/databox/status.class.php b/lib/classes/databox/status.class.php index 5dee255c3b..ed3262c244 100644 --- a/lib/classes/databox/status.class.php +++ b/lib/classes/databox/status.class.php @@ -479,6 +479,15 @@ class databox_status $conn = connection::getPDOConnection(); $status = '0'; + + if(substr($stat1, 0, 2) === '0x') + { + $stat1 = self::hex2bin(substr($stat1, 2)); + } + if(substr($stat2, 0, 2) === '0x') + { + $stat2 = self::hex2bin(substr($stat2, 2)); + } $sql = 'select bin(0b' . trim($stat1) . ' & 0b' . trim($stat2) . ') as result'; @@ -501,6 +510,15 @@ class databox_status $status = '0'; + if(substr($stat1, 0, 2) === '0x') + { + $stat1 = self::hex2bin(substr($stat1, 2)); + } + if(substr($stat2, 0, 2) === '0x') + { + $stat2 = self::hex2bin(substr($stat2, 2)); + } + $sql = 'select bin(0b' . trim($stat1) . ' & ~0b' . trim($stat2) . ') as result'; $stmt = $conn->prepare($sql); @@ -522,6 +540,15 @@ class databox_status $status = '0'; + if(substr($stat1, 0, 2) === '0x') + { + $stat1 = self::hex2bin(substr($stat1, 2)); + } + if(substr($stat2, 0, 2) === '0x') + { + $stat2 = self::hex2bin(substr($stat2, 2)); + } + $sql = 'select bin(0b' . trim($stat1) . ' | 0b' . trim($stat2) . ') as result'; $stmt = $conn->prepare($sql); @@ -539,17 +566,24 @@ class databox_status public static function dec2bin($status) { + $status = (string) $status; + + if(!ctype_digit($status)) + { + throw new \Exception('Non-decimal value'); + } + $conn = connection::getPDOConnection(); - $status = '0'; - - $sql = 'select bin(' . ((int) $status) . ') as result'; + $sql = 'select bin(' . $status . ') as result'; $stmt = $conn->prepare($sql); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); + $status = '0'; + if ($row) { $status = $row['result']; @@ -560,17 +594,28 @@ class databox_status public static function hex2bin($status) { + $status = (string) $status; + if(substr($status, 0, 2) === '0x') + { + $status = substr($status, 2); + } + + if(!ctype_xdigit($status)) + { + throw new \Exception('Non-hexadecimal value'); + } + $conn = connection::getPDOConnection(); - $status = '0'; - - $sql = 'select bin(0x' . trim($status) . ') as result'; + $sql = 'select BIN( CAST( 0x'.trim($status).' AS UNSIGNED ) ) as result'; $stmt = $conn->prepare($sql); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); + $status = '0'; + if ($row) { $status = $row['result']; diff --git a/lib/unitTest/databox/databox_statusTest.php b/lib/unitTest/databox/databox_statusTest.php index 7f3d5cdd0a..b13cc8f8bc 100644 --- a/lib/unitTest/databox/databox_statusTest.php +++ b/lib/unitTest/databox/databox_statusTest.php @@ -14,6 +14,7 @@ class databox_statusTest extends PhraseanetPHPUnitAbstract */ protected $object; protected $databox; + protected static $need_records = 1; public function setUp() { @@ -119,10 +120,10 @@ class databox_statusTest extends PhraseanetPHPUnitAbstract */ public function testOperation_and() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertEquals('0', databox_status::operation_and('0x001','0x010')); + $this->assertEquals('1', databox_status::operation_and('01','11')); + $this->assertEquals('0', databox_status::operation_and('01','10')); + $this->assertEquals('10', databox_status::operation_and('11','10')); } /** @@ -130,10 +131,11 @@ class databox_statusTest extends PhraseanetPHPUnitAbstract */ public function testOperation_and_not() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertEquals('0', databox_status::operation_and_not('0x001','0x011')); + $this->assertEquals('0', databox_status::operation_and_not('01','11')); + $this->assertEquals('1', databox_status::operation_and_not('01','10')); + $this->assertEquals('1', databox_status::operation_and_not('11','10')); + $this->assertEquals('10', databox_status::operation_and_not('10','01')); } /** @@ -141,10 +143,8 @@ class databox_statusTest extends PhraseanetPHPUnitAbstract */ public function testOperation_or() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertEquals('10001', databox_status::operation_or('0x001','0x011')); + $this->assertEquals('11', databox_status::operation_or('01','11')); } /** @@ -152,21 +152,31 @@ class databox_statusTest extends PhraseanetPHPUnitAbstract */ public function testDec2bin() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertEquals('1010', databox_status::dec2bin('10')); + + try + { + } + catch(Exception $e) + { + + } } - /** - * @todo Implement testHex2bin(). - */ public function testHex2bin() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertEquals('10100001', databox_status::hex2bin('0x0A1')); + $this->assertEquals('10100001', databox_status::hex2bin('0A1')); + + try + { + databox_status::hex2bin('G1'); + $this->fail('Should raise an exception'); + } + catch(Exception $e) + { + + } } }