File: MailboxHandlerTest.php

package info (click to toggle)
postfixadmin 3.3.15%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,816 kB
  • sloc: php: 10,120; perl: 1,069; sh: 643; python: 169; xml: 62; sql: 3; makefile: 2
file content (190 lines) | stat: -rw-r--r-- 5,705 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php

class MailboxHandlerTest extends \PHPUnit\Framework\TestCase {
    public function tearDown(): void {
        db_query('DELETE FROM mailbox');
        db_query('DELETE FROM alias');
        db_query('DELETE FROM domain_admins');
        db_query('DELETE FROM domain');

        parent::tearDown();
    }

    public function setUp(): void {
        global $CONF;
        parent::setUp();

        $CONF['quota'] = 'YES';
    }

    public function testBasic() {
        $x = new MailboxHandler();

        $list = $x->getList("");

        $this->assertTrue($list);

        $results = $x->result();

        $this->assertEmpty($results);
    }


    public function testAddingDataEtc() {

        // Fake being an admin.
        $_SESSION = [
            'sessid' => [
                'roles' => ['global-admin']
            ]
        ];
        // Add example.com
        $dh = new DomainHandler(1, 'admin', true);

        $dh->init('example.com');

        $ret = $dh->set(
            [
                'domain' => 'example.com',
                'description' => 'test domain',
                'aliases' => 11,
                'mailboxes' => 12,
                'active' => 1,
                'quota' => 99999911111,
                'maxquota' => 99999999999,
                'backupmx' => 0,
                'default_aliases' => 1
            ]
        );


        $this->assertEmpty($dh->errormsg);
        $this->assertEmpty($dh->infomsg);

        $this->assertTrue($ret);

        $ret = $dh->save();

        $this->assertTrue($ret);

        // Need to add 'admin' as a domain_admin
        db_insert('domain_admins', ['username' => 'admin', 'domain' => 'example.com', 'created' => '2020-01-01', 'active' => 1], ['created'], true);

        $dh = new DomainHandler(0, 'admin', true);
        $dh->getList('');
        $result = $dh->result();

        $this->assertEmpty($dh->infomsg);
        $this->assertEmpty($dh->errormsg);

        $this->assertNotEmpty($result);

        $this->assertEquals('example.com', $result['example.com']['domain']);
        $this->assertEquals('test domain', $result['example.com']['description']);

        $this->assertEquals(11, $result['example.com']['aliases']);
        $this->assertEquals(12, $result['example.com']['mailboxes']); // default aliases.

        $this->assertEquals(4, $result['example.com']['alias_count']); // default aliases.
        $this->assertEquals(0, $result['example.com']['mailbox_count']);
        $this->assertEquals(1, $result['example.com']['active']);

        $x = new MailboxHandler(1, 'admin', true);

        $values = [
            'localpart' => 'david.test',
            'domain' => 'example.com',
            'active' => 1,
            'password' => 'test1234',
            'password2' => 'test1234',
            'name' => 'test person',
            'quota' => 1,
            'welcome_mail' => 0,
            'email_other' => '',
            'username' => 'david.test@example.com',

        ];

        $r = $x->init('david.test@example.com');
        $this->assertTrue($r);
        $x->getList('');
        $list = $x->result();
        $this->assertEquals(0, count($list));

        $x->set($values);
        $x->save();

        $x->getList('');
        $list = $x->result();

        $this->assertEquals(1, count($list), json_encode($x->errormsg));

        $found = false;

        foreach ($list as $key => $details) {
            if ($key == 'david.test@example.com') {
                $this->assertEquals('example.com', $details['domain']);
                $this->assertEquals('david.test@example.com', $details['username']);
                $this->assertEquals('test person', $details['name']);

                $this->assertNotEmpty($details['_modified']);
                $this->assertNotEmpty($details['_created']);

                $this->assertEquals($details['_modified'], $details['_created']); // new data should have them equal.
                $found = true;
                break;
            }
        }

        $this->assertTrue($found, "check output : " . json_encode($list));

        // need to make updated != created.
        sleep(1);

        // Try and edit.

        $h = new MailboxHandler(0, 'admin', true);
        $h->init('david.test@example.com');

        $r = $h->set([
            'password' => '',
            'password2' => '',
            'name' => 'test person 1234',
            'quota' => 123456,
            'active' => 1,
            'email_other' => 'fred@example.com',
            'username' => 'david.test@example.com'
        ]);

        $this->assertEmpty($h->errormsg, json_encode($h->errormsg));
        $this->assertEmpty($h->infomsg);
        $this->assertTrue($r);
        $this->assertTrue($h->save());

        $h->getList('');
        $list = $h->result();
        $this->assertEquals(1, count($list));
        $found = false;
        foreach ($list as $key => $details) {
            if ($key == 'david.test@example.com') {
                // Found!
                $this->assertEquals('example.com', $details['domain']);
                $this->assertEquals('david.test@example.com', $details['username']);
                $this->assertEquals(123456, $details['quota']);
                $this->assertEquals('test person 1234', $details['name']);

                $this->assertNotEmpty($details['_modified']);
                $this->assertNotEmpty($details['_created']);

                $this->assertNotEquals($details['_modified'], $details['_created']);

                $found = true;
                break;
            }
        }

        $this->assertTrue($found);
    }
}

/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */