Added a handler for german bank account information.
authorGunnar Wrobel <p@rdus.de>
Tue, 14 Apr 2009 08:06:59 +0000 (10:06 +0200)
committerGunnar Wrobel <p@rdus.de>
Tue, 14 Apr 2009 17:21:17 +0000 (19:21 +0200)
framework/Kolab_Server/lib/Horde/Kolab/Server/Object/Kolabgermanbankarrangement.php [new file with mode: 0644]
framework/Kolab_Server/test/Horde/Kolab/Server/KolabgermanbankarrangementTest.php [new file with mode: 0644]

diff --git a/framework/Kolab_Server/lib/Horde/Kolab/Server/Object/Kolabgermanbankarrangement.php b/framework/Kolab_Server/lib/Horde/Kolab/Server/Object/Kolabgermanbankarrangement.php
new file mode 100644 (file)
index 0000000..3e75cf9
--- /dev/null
@@ -0,0 +1,155 @@
+<?php
+/**
+ * Represents german bank account information.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package  Kolab_Server
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Server
+ */
+
+/**
+ * This class provides a representation of german bank account
+ * information.
+ *
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package  Kolab_Server
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Server
+ */
+class Horde_Kolab_Server_Object_Kolabgermanbankarrangement extends Horde_Kolab_Server_Object
+{
+    /** Define attributes specific to this object type */
+
+    /** The number of the account */
+    const ATTRIBUTE_NUMBER = 'kolabGermanBankAccountNumber';
+
+    /** The numeric ID of the bank */
+    const ATTRIBUTE_BANKCODE = 'kolabGermanBankCode';
+
+    /** Account holder */
+    const ATTRIBUTE_HOLDER = 'kolabGermanBankAccountHolder';
+
+    /** Name of the bank */
+    const ATTRIBUTE_BANKNAME = 'kolabGermanBankName';
+
+    /** Additional information */
+    const ATTRIBUTE_INFO = 'kolabGermanBankAccountInfo';
+
+    /** The uid of the owner of this account */
+    const ATTRIBUTE_OWNERUID = 'kolabGermanBankAccountOwnerUid';
+
+    /** The specific object class of this object type */
+    const OBJECTCLASS_KOLABGERMANBANKARRANGEMENT = 'kolabGermanBankArrangement';
+
+    /**
+     * A structure to initialize the attribute structure for this class.
+     *
+     * @var array
+     */
+    static public $init_attributes = array(
+        'defined' => array(
+            self::ATTRIBUTE_NUMBER,
+            self::ATTRIBUTE_BANKCODE,
+            self::ATTRIBUTE_HOLDER,
+            self::ATTRIBUTE_BANKNAME,
+            self::ATTRIBUTE_INFO,
+        ),
+        'derived' => array(
+            self::ATTRIBUTE_OWNERUID => array(
+                'method' => 'getParentUid',
+            ),
+        ),
+        'required' => array(
+            self::ATTRIBUTE_NUMBER,
+            self::ATTRIBUTE_BANKCODE,
+        ),
+        'object_classes' => array(
+            self::OBJECTCLASS_KOLABGERMANBANKARRANGEMENT,
+        ),
+    );
+
+    /**
+     * Generates an ID for the given information.
+     *
+     * @param array $info The data of the object.
+     *
+     * @static
+     *
+     * @return string|PEAR_Error The ID.
+     */
+    public function generateId(&$info)
+    {
+        if (!isset($info[self::ATTRIBUTE_OWNERUID])) {
+            throw new Horde_Kolab_Server_Exception(_("No parent object provided!"),
+                                                   Horde_Kolab_Server_Exception::INVALID_INFORMATION);
+        }
+
+        if (is_array($info[self::ATTRIBUTE_OWNERUID])) {
+            $uid = $info[self::ATTRIBUTE_OWNERUID][0];
+        } else {
+            $uid = $info[self::ATTRIBUTE_OWNERUID];
+        }
+
+        $object = $this->server->fetch($uid);
+        if (!$object->exists()) {
+            throw new Horde_Kolab_Server_Exception(sprintf(_("The parent object %s does not exist!"),
+                                                           $uid),
+                                                   Horde_Kolab_Server_Exception::INVALID_INFORMATION);
+        }
+        if (!isset($info[self::ATTRIBUTE_NUMBER])) {
+            throw new Horde_Kolab_Server_Exception(_("No account number given!"),
+                                                   Horde_Kolab_Server_Exception::INVALID_INFORMATION);
+        }
+
+        if (is_array($info[self::ATTRIBUTE_NUMBER])) {
+            $number = $info[self::ATTRIBUTE_NUMBER][0];
+        } else {
+            $number = $info[self::ATTRIBUTE_NUMBER];
+        }
+
+        $base = substr($uid, 0, strpos($uid, $this->server->getBaseUid()) - 1);
+
+        return self::ATTRIBUTE_NUMBER . '=' . $this->server->structure->quoteForUid($number) . ',' . $base;
+    }
+
+    /**
+     * Returns the set of search operations supported by this object type.
+     *
+     * @return array An array of supported search operations.
+     */
+    static public function getSearchOperations()
+    {
+        $searches = array(
+            'accountsForMail',
+        );
+        return $searches;
+    }
+
+    /**
+     * Returns the UIDs of the bank accounts for the user with the given mail
+     * address.
+     *
+     * @param Horde_Kolab_Server $server The server to query.
+     * @param string             $mail   Search objects with this mail alias.
+     *
+     * @return mixed The UIDs or false if there was no result.
+     *
+     * @throws Horde_Kolab_Server_Exception
+     */
+    static public function accountsForMail($server, $mail)
+    {
+        $uid = $server->uidForMail($mail, Horde_Kolab_Server_Object::RESULT_SINGLE);
+        return self::objectsForUid($server, $uid, self::OBJECTCLASS_KOLABGERMANBANKARRANGEMENT);
+    }
+
+}
\ No newline at end of file
diff --git a/framework/Kolab_Server/test/Horde/Kolab/Server/KolabgermanbankarrangementTest.php b/framework/Kolab_Server/test/Horde/Kolab/Server/KolabgermanbankarrangementTest.php
new file mode 100644 (file)
index 0000000..17949b1
--- /dev/null
@@ -0,0 +1,193 @@
+<?php
+/**
+ * Test the kolabGermanBankArrangement object.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package  Kolab_Server
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Server
+ */
+
+/**
+ * The Autoloader allows us to omit "require/include" statements.
+ */
+require_once 'Horde/Autoloader.php';
+
+/**
+ * Test the kolabGermanBankArrangement object.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package  Kolab_Server
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Server
+ */
+class Horde_Kolab_Server_KolabgermanbankarrangementTest extends Horde_Kolab_Test_Server
+{
+    /**
+     * Objects used within this test
+     *
+     * @var array
+     */
+    private $objects = array(
+        /* Default bank account owner */
+        array(
+            'type' => 'Horde_Kolab_Server_Object_Kolabinetorgperson',
+            Horde_Kolab_Server_Object_Kolabinetorgperson::ATTRIBUTE_GIVENNAME    => 'Frank',
+            Horde_Kolab_Server_Object_Kolabinetorgperson::ATTRIBUTE_SN           => 'Mustermann',
+            Horde_Kolab_Server_Object_Kolabinetorgperson::ATTRIBUTE_USERPASSWORD => 'Kolab_Server_OrgPersonTest_123',
+        ),
+        /* Default account */
+        array(
+            'type' => 'Horde_Kolab_Server_Object_Kolabgermanbankarrangement',
+            Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_NUMBER   => '0123456789',
+            Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_BANKCODE => '1111111',
+        ),
+    );
+
+    /**
+     * Provide different server types.
+     *
+     * @return array The different server types.
+     */
+    public function &provideServers()
+    {
+        $servers = array();
+        /**
+         * We always use the test server
+         */
+        $servers[] = array($this->prepareEmptyKolabServer());
+        if (false) {
+            $real = $this->prepareLdapKolabServer();
+            if (!empty($real)) {
+                $servers[] = array($real);
+            }
+        }
+        return $servers;
+    }
+
+    /**
+     * Test ID generation for a person.
+     *
+     * @dataProvider provideServers
+     *
+     * @return NULL
+     */
+    public function testGenerateId($server)
+    {
+        $person = $this->assertAdd($server, $this->objects[0],
+                                   array(Horde_Kolab_Server_Object_Kolabinetorgperson::ATTRIBUTE_SID => ''));
+        $account_data = $this->objects[1];
+        $account_data[Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_OWNERUID] = $person->getUid();
+        $a = new Horde_Kolab_Server_Object_Kolabgermanbankarrangement($server, null, $account_data);
+        $this->assertContains(Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_NUMBER . '=' . $this->objects[1][Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_NUMBER],
+                              $a->get(Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_UID));
+    }
+
+    /**
+     * Test adding an invalid Account.
+     *
+     * @dataProvider provideServers
+     * @expectedException Horde_Kolab_Server_Exception
+     *
+     * @return NULL
+     */
+    public function testAddInvalidAccount($server)
+    {
+        $result = $server->add($this->objects[1]);
+    }
+
+    /**
+     * Test handling easy attributes.
+     *
+     * @dataProvider provideServers
+     *
+     * @return NULL
+     */
+    public function testEasyAttributes($server)
+    {
+        $person = $this->assertAdd($server, $this->objects[0],
+                                   array(Horde_Kolab_Server_Object_Kolabinetorgperson::ATTRIBUTE_SID => ''));
+        $account_data = $this->objects[1];
+        $account_data[Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_OWNERUID] = $person->getUid();
+        $account = $this->assertAdd($server, $account_data,
+                                   array(Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_OWNERUID => $person->getUid()));
+        $this->assertEasyAttributes($account, $server,
+                                    array(
+                                        Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_BANKCODE => array(
+                                            '01234567890123456789',
+                                            '0',
+                                            '101',
+                                            null,
+                                            '',
+                                            array('101', '202'),
+                                        ),
+                                        Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_HOLDER => array(
+                                            'something',
+                                            'somewhere',
+                                            null,
+                                            array('a', 'b'),
+                                            '',
+                                        ),
+                                        Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_BANKNAME => array(
+                                            'something',
+                                            'somewhere',
+                                            null,
+                                            array('a', 'b'),
+                                            '',
+                                        ),
+                                        Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_INFO => array(
+                                            'something',
+                                            'somewhere',
+                                            null,
+                                            array('a', 'b'),
+                                            '',
+                                        ),
+                                    )
+        );
+    }
+
+    /**
+     * Test modifying the account number of an account. This should have an
+     * effect on the UID of the object and needs to rename the object.
+     *
+     * @dataProvider provideServers
+     *
+     * @return NULL
+     */
+    public function testModifyAccountNumber($server)
+    {
+        $person = $this->assertAdd($server, $this->objects[0],
+                                   array(Horde_Kolab_Server_Object_Kolabinetorgperson::ATTRIBUTE_SID => ''));
+        $account_data = $this->objects[1];
+        $account_data[Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_OWNERUID] = $person->getUid();
+        $account = $server->add($account_data);
+        $this->assertNoError($account);
+
+        $account = $server->fetch($account->getUid());
+        $this->assertNoError($account);
+
+        $this->assertEquals($this->objects[1][Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_NUMBER],
+                            $account->get(Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_NUMBER));
+
+        $result = $account->save(array(Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_NUMBER => '66666666'));
+        $this->assertNoError($result);
+
+        $account = $server->fetch($account->getUid());
+        $this->assertNoError($account);
+
+        $this->assertEquals($account->get(Horde_Kolab_Server_Object_Kolabgermanbankarrangement::ATTRIBUTE_NUMBER),
+                            '66666666');
+
+        $result = $server->delete($account->getUid());
+        $this->assertNoError($result);
+    }
+}