PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.6 KiB

  1. <?php
  2. class MailboxHandlerTest extends \PHPUnit\Framework\TestCase {
  3. public function tearDown(): void {
  4. db_query('DELETE FROM mailbox');
  5. db_query('DELETE FROM alias');
  6. db_query('DELETE FROM domain_admins');
  7. db_query('DELETE FROM domain');
  8. parent::tearDown();
  9. }
  10. public function setUp(): void {
  11. global $CONF;
  12. parent::setUp();
  13. $CONF['quota'] = 'YES';
  14. }
  15. public function testBasic() {
  16. $x = new MailboxHandler();
  17. $list = $x->getList("");
  18. $this->assertTrue($list);
  19. $results = $x->result();
  20. $this->assertEmpty($results);
  21. }
  22. public function testAddingDataEtc() {
  23. // Fake being an admin.
  24. $_SESSION = [
  25. 'sessid' => [
  26. 'roles' => ['global-admin']
  27. ]
  28. ];
  29. // Add example.com
  30. $dh = new DomainHandler(1, 'admin', true);
  31. $dh->init('example.com');
  32. $ret = $dh->set(
  33. [
  34. 'domain' => 'example.com',
  35. 'description' => 'test domain',
  36. 'aliases' => 11,
  37. 'mailboxes' => 12,
  38. 'active' => 1,
  39. 'quota' => 99999911111,
  40. 'maxquota' => 99999999999,
  41. 'backupmx' => 0,
  42. 'default_aliases' => 1
  43. ]
  44. );
  45. $this->assertEmpty($dh->errormsg);
  46. $this->assertEmpty($dh->infomsg);
  47. $this->assertTrue($ret);
  48. $ret = $dh->save();
  49. $this->assertTrue($ret);
  50. // Need to add 'admin' as a domain_admin
  51. db_insert('domain_admins', ['username' => 'admin', 'domain' => 'example.com', 'created' => '2020-01-01', 'active' => 1], ['created'], true);
  52. $dh = new DomainHandler(0, 'admin', true);
  53. $dh->getList('');
  54. $result = $dh->result();
  55. $this->assertEmpty($dh->infomsg);
  56. $this->assertEmpty($dh->errormsg);
  57. $this->assertNotEmpty($result);
  58. $this->assertEquals('example.com', $result['example.com']['domain']);
  59. $this->assertEquals('test domain', $result['example.com']['description']);
  60. $this->assertEquals(11, $result['example.com']['aliases']);
  61. $this->assertEquals(12, $result['example.com']['mailboxes']); // default aliases.
  62. $this->assertEquals(4, $result['example.com']['alias_count']); // default aliases.
  63. $this->assertEquals(0, $result['example.com']['mailbox_count']);
  64. $this->assertEquals(1, $result['example.com']['active']);
  65. $x = new MailboxHandler(1, 'admin', true);
  66. $values = [
  67. 'localpart' => 'david.test',
  68. 'domain' => 'example.com',
  69. 'active' => 1,
  70. 'password' => 'test1234',
  71. 'password2' => 'test1234',
  72. 'name' => 'test person',
  73. 'quota' => 1,
  74. 'welcome_mail' => 0,
  75. 'email_other' => '',
  76. 'username' => 'david.test@example.com',
  77. ];
  78. $r = $x->init('david.test@example.com');
  79. $this->assertTrue($r);
  80. $x->getList('');
  81. $list = $x->result();
  82. $this->assertEquals(0, count($list));
  83. $x->set($values);
  84. $x->save();
  85. $x->getList('');
  86. $list = $x->result();
  87. $this->assertEquals(1, count($list), json_encode($x->errormsg));
  88. $found = false;
  89. foreach ($list as $key => $details) {
  90. if ($key == 'david.test@example.com') {
  91. $this->assertEquals('example.com', $details['domain']);
  92. $this->assertEquals('david.test@example.com', $details['username']);
  93. $this->assertEquals('test person', $details['name']);
  94. $this->assertNotEmpty($details['_modified']);
  95. $this->assertNotEmpty($details['_created']);
  96. $this->assertEquals($details['_modified'], $details['_created']); // new data should have them equal.
  97. $found = true;
  98. break;
  99. }
  100. }
  101. $this->assertTrue($found, "check output : " . json_encode($list));
  102. // need to make updated != created.
  103. sleep(1);
  104. // Try and edit.
  105. $h = new MailboxHandler(0, 'admin', true);
  106. $h->init('david.test@example.com');
  107. $r = $h->set([
  108. 'password' => '',
  109. 'password2' => '',
  110. 'name' => 'test person 1234',
  111. 'quota' => 123456,
  112. 'active' => 1,
  113. 'email_other' => 'fred@example.com',
  114. 'username' => 'david.test@example.com'
  115. ]);
  116. $this->assertEmpty($h->errormsg, json_encode($h->errormsg));
  117. $this->assertEmpty($h->infomsg);
  118. $this->assertTrue($r);
  119. $this->assertTrue($h->save());
  120. $h->getList('');
  121. $list = $h->result();
  122. $this->assertEquals(1, count($list));
  123. $found = false;
  124. foreach ($list as $key => $details) {
  125. if ($key == 'david.test@example.com') {
  126. // Found!
  127. $this->assertEquals('example.com', $details['domain']);
  128. $this->assertEquals('david.test@example.com', $details['username']);
  129. $this->assertEquals(123456, $details['quota']);
  130. $this->assertEquals('test person 1234', $details['name']);
  131. $this->assertNotEmpty($details['_modified']);
  132. $this->assertNotEmpty($details['_created']);
  133. $this->assertNotEquals($details['_modified'], $details['_created']);
  134. $found = true;
  135. break;
  136. }
  137. }
  138. $this->assertTrue($found);
  139. }
  140. }
  141. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */