PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/APITest.php

https://github.com/B-Prod/Mailin
PHP | 709 lines | 433 code | 106 blank | 170 comment | 24 complexity | 9a36ecdf4e4001eaca8e73b630e0e2a8 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Provide PHPUnit tests for Mailin API class.
  5. */
  6. namespace Mailin\Tests;
  7. use Mailin\API as Mailin;
  8. use Mailin\Attribute as MA;
  9. use Mailin\Log;
  10. use Mailin\User;
  11. /**
  12. * Test class for the Mailin API class.
  13. *
  14. * @backupGlobals disabled
  15. * @backupStaticAttributes disabled
  16. */
  17. class APITest extends \PHPUnit_Framework_TestCase {
  18. /**
  19. * The API key for the test account.
  20. */
  21. const API_KEY = 'sdfg545nh';
  22. /**
  23. * The Mailin API instance.
  24. *
  25. * @var Mailin
  26. */
  27. protected $mailin;
  28. /**
  29. * The total number of calls to the Mailin server.
  30. *
  31. * @var int
  32. */
  33. protected static $apiCallsCount;
  34. /**
  35. * @inheritdoc
  36. */
  37. protected function setUp() {
  38. $this->mailin = new Mailin(self::API_KEY);
  39. }
  40. /**
  41. * Assert that the number of API call is correct.
  42. *
  43. * @param $increment
  44. * How much the internal counter of API call should be incremented.
  45. */
  46. protected function assertCallcount($increment = 1) {
  47. if ($increment) {
  48. self::$apiCallsCount += $increment;
  49. }
  50. $this->assertEquals(self::$apiCallsCount, Log::countApiCalls(), 'API calls count does not match.');
  51. }
  52. /**
  53. * Create a random folder name that does not exist in Mailin server.
  54. */
  55. protected function createFolderName() {
  56. $folders = array_map(function ($folder) { return $folder['name']; }, $this->mailin->getFolders());
  57. self::$apiCallsCount++;
  58. do {
  59. $name = \MailinTestHelper::randomName();
  60. }
  61. while ($folders && in_array($name, $folders));
  62. return $name;
  63. }
  64. /**
  65. * Create a random list name that does not exist in Mailin server.
  66. *
  67. * @param $parentId
  68. * The parent folder ID.
  69. */
  70. protected function createListName($parentId) {
  71. $lists = array_map(function ($list) { return $list['name']; }, $this->mailin->getListsFromFolder($parentId));
  72. self::$apiCallsCount++;
  73. do {
  74. $name = \MailinTestHelper::randomName();
  75. }
  76. while ($lists && in_array($name, $lists));
  77. return $name;
  78. }
  79. /**
  80. * @covers Mailin\API::getFolders
  81. */
  82. public function testGetFolders() {
  83. $folders = $this->mailin->getFolders();
  84. $this->assertInternalType('array', $folders, 'Folder fetch operation failed.');
  85. $this->assertCallcount();
  86. }
  87. /**
  88. * @covers Mailin\API::addFolder
  89. * @depends testGetFolders
  90. */
  91. public function testAddFolder() {
  92. $folderName = $this->createFolderName();
  93. $folderId = $this->mailin->addFolder($folderName);
  94. $this->assertInternalType('int', $folderId, "Creation of folder $folderName failed.");
  95. $this->assertCallcount(2); // 2 API calls.
  96. // Try to create a folder with the same name.
  97. $this->assertFalse($this->mailin->addFolder($folderName), "Creation of a folder with the same name $folderName should fail.");
  98. $this->assertCallcount(); // Only 1 API call, since the first call to findFoldersByName() stops the process.
  99. // Force the creation of a new folder even if a folder with the same name exists.
  100. $folderId2 = $this->mailin->addFolder($folderName, TRUE);
  101. $this->assertInternalType('int', $folderId, "Forcing the creation of folder with the same name $folderName failed.");
  102. $this->assertNotEquals($folderId, $folderId2, 'The two folders should not have the same ID.');
  103. $this->assertCallcount();
  104. return array($folderId => $folderName, $folderId2 => $folderName);
  105. }
  106. /**
  107. * @covers Mailin\API::findFolder
  108. * @depends testAddFolder
  109. */
  110. public function testFindFolder($folders) {
  111. list($folderId, $folderName) = each($folders);
  112. $folder = $this->mailin->findFolder($folderId);
  113. $this->assertInternalType('array', $folder, 'Find folder operation failed.');
  114. $this->assertEquals($folderId, $folder['id'], 'Returned folder has not the expected ID.');
  115. $this->assertEquals($folderName, $folder['name'], 'Returned folder has not the expected name.');
  116. $this->assertCallcount();
  117. }
  118. /**
  119. * @covers Mailin\API::findFoldersByName
  120. * @depends testAddFolder
  121. */
  122. public function testFindFoldersByName($folders) {
  123. // Search an unexisting folder.
  124. $this->assertEquals(array(), $this->mailin->findFoldersByName($this->createFolderName()), 'Unexisting folder search should return FALSE.');
  125. $this->assertCallcount();
  126. list($folderId, $folderName) = each($folders);
  127. $results = array_fill_keys(array_keys($this->mailin->findFoldersByName($folderName)), $folderName);
  128. ksort($results);
  129. ksort($folders);
  130. $this->assertEquals($results, $folders, 'The search should return the two newly created folders and only that.');
  131. $this->assertCallcount();
  132. }
  133. /**
  134. * @covers Mailin\API::getLists
  135. */
  136. public function testGetLists() {
  137. $lists = $this->mailin->getLists();
  138. $this->assertInternalType('array', $lists, 'List fetch operation failed.');
  139. $this->assertCallcount();
  140. }
  141. /**
  142. * @covers Mailin\API::addList
  143. * @depends testAddFolder
  144. */
  145. public function testAddList(array $folders) {
  146. list($folderId, $folderId2) = array_keys($folders);
  147. $listName = $this->createListName($folderId);
  148. $listId = $this->mailin->addList($listName, $folderId);
  149. $this->assertInternalType('int', $listId, "Creation of list $listId failed.");
  150. $this->assertCallcount(2); // 2 API calls.
  151. // Create a second list in the same folder.
  152. $listName2 = $this->createListName($folderId);
  153. $listId2 = $this->mailin->addList($listName2, $folderId);
  154. $this->assertInternalType('int', $listId2, "Creation of list $listId2 failed.");
  155. $this->assertCallcount(2); // 2 API calls.
  156. // Create a list with the same name, in the same folder.
  157. $this->assertFalse($this->mailin->addList($listName, $folderId), "Creation of a list with the same name $listName in the same folder should fail.");
  158. $this->assertCallcount(); // Only 1 API call, since the first call to findListsByName() stops the process.
  159. // Force the creation of a new list even if a list with the same name exists in the same folder.
  160. $listId3 = $this->mailin->addList($listName, $folderId, TRUE);
  161. $this->assertInternalType('int', $listId3, "Forcing the creation of list with the same name $listName in the same folder failed.");
  162. $this->assertFalse(in_array($listId3, array($listId, $listId2)), 'The lists should not have the same ID.');
  163. $this->assertCallcount();
  164. // Create a list with the same name, in a different folder, without forcing.
  165. $listId4 = $this->mailin->addList($listName, $folderId2);
  166. $this->assertInternalType('int', $listId4, "Creation of list with the same name $listName in a different folder failed.");
  167. $this->assertFalse(in_array($listId4, array($listId, $listId2, $listId3)), 'The three lists should not have the same ID.');
  168. $this->assertCallcount(2); // 2 API calls.
  169. // Check now that the created lists are present on the Mailin server.
  170. $lists = array_fill_keys(array($listId, $listId2, $listId3, $listId4), TRUE);
  171. $this->assertTrue(count(array_intersect_key($lists, $this->mailin->getLists())) === 4, 'All the expected lists are not present on the Mailin server.');
  172. $this->assertCallcount();
  173. return array(
  174. $folderId => array($listId => $listName, $listId2 => $listName2, $listId3 => $listName),
  175. $folderId2 => array($listId4 => $listName),
  176. );
  177. }
  178. /**
  179. * @covers Mailin\API::findLists
  180. * @depends testAddList
  181. */
  182. public function testFindList(array $foldersLists) {
  183. list($folderId, $lists) = each($foldersLists);
  184. list($listId, $listName) = each($lists);
  185. $list = $this->mailin->findList($listId);
  186. $this->assertInternalType('array', $list, 'Find list operation failed.');
  187. $this->assertEquals($listId, $list['id'], 'Returned list has not the expected ID.');
  188. $this->assertEquals($listName, $list['name'], 'Returned list has not the expected name.');
  189. $this->assertEquals($folderId, $list['folder_id'], 'Returned list has not the expected parent folder.');
  190. $this->assertCallcount();
  191. // Try wrong ID.
  192. $wrongId = rand(10000, 20000);
  193. $this->assertFalse($this->mailin->findList($wrongId), 'A unexisting ID should not return anything.');
  194. $this->assertCallcount();
  195. // Try existing folder ID.
  196. $this->assertFalse($this->mailin->findList($folderId), 'An existing folder ID should not return any list.');
  197. $this->assertCallcount();
  198. }
  199. /**
  200. * @covers Mailin\API::getListsFromFolder
  201. * @depends testAddList
  202. */
  203. function testGetListsFromFolder(array $foldersLists) {
  204. foreach ($foldersLists as $folderId => $lists) {
  205. $expectedIds = array_keys($lists);
  206. $count = sizeof($lists);
  207. $results = $this->mailin->getListsFromFolder($folderId);
  208. $resultIds = array_keys($results);
  209. sort($expectedIds);
  210. sort($resultIds);
  211. $this->assertEquals($resultIds, $expectedIds, "The search should return the $count list(s) and only that.");
  212. $this->assertCallcount();
  213. // Check the list properties.
  214. foreach ($results as $id => $list) {
  215. $this->assertEquals($lists[$id], $list['name'], 'Returned list has not the expected name.');
  216. $this->assertEquals($folderId, $list['folder_id'], 'Returned list has not the expected parent folder.');
  217. }//end foreach
  218. }//end foreach
  219. }
  220. /**
  221. * @covers Mailin\API::findListsByName
  222. * @depends testAddList
  223. */
  224. public function testFindListsByName(array $foldersLists) {
  225. foreach ($foldersLists as $folderId => $lists) {
  226. foreach (array_count_values($lists) as $listName => $count) {
  227. $expectedIds = array_keys($lists, $listName);
  228. $results = $this->mailin->findListsByName($listName, $folderId);
  229. $resultIds = array_keys($results);
  230. sort($expectedIds);
  231. sort($resultIds);
  232. $this->assertEquals($resultIds, $expectedIds, "The search should return the $count list(s) and only that.");
  233. $this->assertCallcount();
  234. // Check the list properties.
  235. foreach ($results as $list) {
  236. $this->assertEquals($listName, $list['name'], 'Returned list has not the expected name.');
  237. $this->assertEquals($folderId, $list['folder_id'], 'Returned list has not the expected parent folder.');
  238. }//end foreach
  239. }//end foreach
  240. }//end foreach
  241. }
  242. /**
  243. * @covers Mailin\API::getAttributes
  244. */
  245. public function testGetAttributes() {
  246. $result = $this->mailin->getAttributes();
  247. $this->assertInternalType('array', $result);
  248. // There is at least some locked attributes, so the result should not
  249. // be empty.
  250. $this->assertNotEmpty($result, 'Getting attribute list failed.');
  251. $this->assertCallcount(1);
  252. $this->assertEquals(array(), $this->mailin->getAttributes(array('unexisting-type')));
  253. $this->assertCallcount(0);
  254. }
  255. /**
  256. * @covers Mailin\API::addAttributes
  257. */
  258. public function testAddAttributes() {
  259. $callback = array('\MailinTestHelper', 'randomName');
  260. $args = array(8, 'TEST-', 1, FALSE, '_-');
  261. $attributes = array(
  262. MA\AttributeInterface::ATTRIBUTE_LIST_NORMAL => array(
  263. new MA\Normal(array(
  264. 'name' => call_user_func_array($callback, $args),
  265. 'type' => MA\AttributeInterface::ATTRIBUTE_DATA_TYPE_TEXT,
  266. )),
  267. new MA\Normal(array(
  268. 'name' => call_user_func_array($callback, $args),
  269. 'type' => MA\AttributeInterface::ATTRIBUTE_DATA_TYPE_NUMBER,
  270. )),
  271. new MA\Normal(array(
  272. 'name' => call_user_func_array($callback, $args),
  273. 'type' => MA\AttributeInterface::ATTRIBUTE_DATA_TYPE_DATE,
  274. )),
  275. ),
  276. MA\AttributeInterface::ATTRIBUTE_LIST_CATEGORY => array(
  277. new MA\Category(array(
  278. 'name' => call_user_func_array($callback, $args),
  279. 'enumeration' => array(
  280. \MailinTestHelper::randomName(),
  281. \MailinTestHelper::randomName(),
  282. \MailinTestHelper::randomName(),
  283. ),
  284. )),
  285. ),
  286. );
  287. $this->assertTrue($this->mailin->addAttributes($attributes), 'Attributes creation failed.');
  288. $this->assertCallcount();
  289. $results = $this->mailin->getAttributes(array(MA\AttributeInterface::ATTRIBUTE_LIST_NORMAL, MA\AttributeInterface::ATTRIBUTE_LIST_CATEGORY));
  290. $this->assertTrue(sizeof($results) === 2, 'Getting attribute list failed.');
  291. $this->assertCallcount(1);
  292. $missing = $attributes;
  293. $filter = new MA\Filter($results);
  294. // Check attributes correspondence.
  295. foreach ($attributes as $attributeType => $attributeInstances) {
  296. foreach ($attributeInstances as $key => $attribute) {
  297. $filter->setFilter('type', $attributeType)->setFilter('name', $attribute->getName());
  298. if ($found = $filter->getOne()) {
  299. switch ($attributeType) {
  300. case MA\AttributeInterface::ATTRIBUTE_LIST_NORMAL:
  301. if ($found->getDataType() === $attribute->getDataType()) {
  302. unset($missing[$attributeType][$key]);
  303. }
  304. break;
  305. case MA\AttributeInterface::ATTRIBUTE_LIST_CATEGORY:
  306. if (!array_diff($attribute->getEnumeration(TRUE), $found->getEnumeration(TRUE))) {
  307. unset($missing[$attributeType][$key]);
  308. }
  309. break;
  310. }//end switch
  311. }
  312. }//end foreach
  313. }//end foreach
  314. $this->assertEmpty(array_filter($missing), 'Some attributes have not been created.');
  315. // Try to save invalid attributes.
  316. $validAttributeName = \MailinTestHelper::randomName(8, 'TEST-', 1, FALSE, '_-');
  317. $invalid = array(
  318. array(
  319. 'attribute' => array(),
  320. 'message' => 'Attribute creation using an invalid attribute type should fail.',
  321. ),
  322. array(
  323. 'attribute' => array(MA\AttributeInterface::ATTRIBUTE_LIST_NORMAL => 'not-array'),
  324. 'message' => 'Attribute creation using an invalid structure should fail.',
  325. ),
  326. array(
  327. 'attribute' => array(MA\AttributeInterface::ATTRIBUTE_LIST_NORMAL => array(
  328. new MA\Normal(array('name' => 'INVALID NAME', 'type' => MA\AttributeInterface::ATTRIBUTE_DATA_TYPE_TEXT)),
  329. )),
  330. 'message' => 'Attribute creation using an invalid name should fail.',
  331. ),
  332. array(
  333. 'attribute' => array(MA\AttributeInterface::ATTRIBUTE_LIST_NORMAL => array(
  334. new MA\Normal(array('name' => $validAttributeName, 'type' => 'INVALID_DATA_TYPE')),
  335. )),
  336. 'message' => 'Attribute creation using an unexisting data type should fail.',
  337. ),
  338. array(
  339. 'attribute' => array(MA\AttributeInterface::ATTRIBUTE_LIST_NORMAL => array(
  340. new MA\Normal(array('name' => $validAttributeName, 'type' => MA\AttributeInterface::ATTRIBUTE_DATA_TYPE_ID)),
  341. )),
  342. 'message' => 'Attribute creation using an unsupported data type should fail.',
  343. ),
  344. array(
  345. 'attribute' => array(MA\AttributeInterface::ATTRIBUTE_LIST_CATEGORY => array(
  346. new MA\Category(array('name' => $validAttributeName)),
  347. )),
  348. 'message' => 'Category attribute creation using no enumeration should fail.',
  349. ),
  350. );
  351. foreach ($invalid as $test) {
  352. $test += array('increment' => 0);
  353. $this->assertFalse($this->mailin->addAttributes($test['attribute']), $test['message']);
  354. $this->assertCallcount($test['increment']);
  355. }//end foreach
  356. // @todo test adding a transactional attribute
  357. // @todo test adding a calculated attribute
  358. // @todo test adding a computed attribute
  359. return $attributes;
  360. }
  361. /**
  362. * @covers Mailin\API::deleteAttributes
  363. * @depends testAddAttributes
  364. */
  365. public function testDeleteAttributes(array $attributes) {
  366. $this->assertTrue($this->mailin->deleteAttributes($attributes), 'Attributes deletion failed.');
  367. $this->assertCallcount();
  368. // Check if the attributes were really deleted.
  369. $results = $this->mailin->getAttributes();
  370. $this->assertCallcount();
  371. $filter = new MA\Filter($results);
  372. foreach ($attributes as $attributeType => $instances) {
  373. foreach ($instances as $attribute) {
  374. $filter->setFilters(array('type' => $attributeType, 'name' => $attribute->getName()));
  375. if ($found = $filter->getOne()) {
  376. $this->fail('Atribute with name ' . $found->getName() . ' still exists while it should have been deleted.');
  377. }
  378. }
  379. }//end foreach
  380. }
  381. /**
  382. * @covers Mailin\API::saveUser
  383. * @depends testAddList
  384. */
  385. public function testSaveUser(array $foldersLists) {
  386. list($listId, $listId2, $listId3) = array_keys(reset($foldersLists));
  387. $userEmail = \MailinTestHelper::randomEmail();
  388. $userId = $this->mailin->saveUser($userEmail, array($listId => TRUE));
  389. $this->assertTrue(is_numeric($userId), "Creation of user $userEmail failed.");
  390. $result = $this->mailin->getUserStatus(array($userEmail));
  391. $this->assertInternalType('array', $result, "Impossible to retrieve user $userEmail related data.");
  392. $this->assertEquals(reset($result), 0, "User $userEmail status is incorrect.");
  393. $this->assertCallcount(2);
  394. // Create now a user who is blacklisted.
  395. $userEmail2 = \MailinTestHelper::randomEmail();
  396. $userId2 = $this->mailin->saveUser($userEmail2, array($listId => TRUE), array(), TRUE);
  397. $this->assertTrue(is_numeric($userId2), "Creation of user $userEmail2 failed.");
  398. $result = $this->mailin->getUserStatus(array($userEmail2));
  399. $this->assertInternalType('array', $result, "Impossible to retrieve user $userEmail2 related data.");
  400. $this->assertEquals(reset($result), 1, "User $userEmail2 status is incorrect.");
  401. $this->assertCallcount(2);
  402. // Create a user who is registered on an unexisting list.
  403. do {
  404. $unexistingListId = rand(1, 9999);
  405. self::$apiCallsCount++;
  406. }
  407. while (FALSE !== $this->mailin->getList($unexistingListId));
  408. $this->assertCallcount(0);
  409. $userEmail3 = \MailinTestHelper::randomEmail();
  410. $userId3 = $this->mailin->saveUser($userEmail3, array($unexistingListId => TRUE));
  411. $this->assertTrue(is_numeric($userId3), "Creation of user $userEmail3 failed.");
  412. $user = $this->mailin->getUser($userEmail3);
  413. $this->assertTrue(is_object($user) && $user instanceof User, "Impossible to retrieve user $userEmail3 related data.");
  414. $this->assertFalse($user->isRegistered($unexistingListId), "User $userEmail3 should not be registred on a unexisting list.");
  415. $this->assertCallcount(2);
  416. // Create a user without spĂŠcifying any list ID.
  417. // Those tests were removed since the required state of 'lists' for new
  418. // users was changed.
  419. //$userEmail4 = \MailinTestHelper::randomEmail();
  420. //$this->assertFalse($this->mailin->saveUser($userEmail4, array()), "Creation of user $userEmail4 without list ID should fail.");
  421. //$this->assertCallcount();
  422. //$this->assertFalse($this->mailin->saveUser($userEmail4, array(0 => TRUE)), "Creation of user $userEmail4 with 0 as list ID should fail.");
  423. //$this->assertCallcount();
  424. //$this->assertFalse($this->mailin->saveUser($userEmail4, array('' => TRUE)), "Creation of user $userEmail4 with empty string as list ID should fail.");
  425. //$this->assertCallcount();
  426. // Save a user registered on multiple lists.
  427. $userEmail5 = \MailinTestHelper::randomEmail();
  428. $userId5 = $this->mailin->saveUser($userEmail5, array($listId => TRUE, $listId2 => TRUE, $listId3 => TRUE));
  429. $this->assertTrue(is_numeric($userId5), "Creation of user $userEmail5 registered on multiple lists failed.");
  430. $this->assertCallcount();
  431. // Verify that the created user is registered on the all lists.
  432. $user = $this->mailin->getUser($userEmail5);
  433. $this->assertTrue(is_object($user) && $user instanceof User, "Impossible to retrieve user $userEmail5 related data.");
  434. $retrievedLists = array_keys($user->getLists());
  435. sort($retrievedLists);
  436. $savedLists = array($listId, $listId2, $listId3);
  437. sort($savedLists);
  438. $this->assertEquals($retrievedLists, $savedLists, 'Fetched user lists does not match the ones that were saved.');
  439. $this->assertCallcount();
  440. // Unsubscribe the user from some lists.
  441. $userId5 = $this->mailin->saveUser($userEmail5, array($listId => FALSE, $listId2 => FALSE));
  442. $this->assertTrue(is_numeric($userId5), "Creation of user $userEmail5 registered on multiple lists failed.");
  443. $this->assertCallcount();
  444. $user = $this->mailin->getUser($userEmail5);
  445. $this->assertTrue(is_object($user) && $user instanceof User, "Impossible to retrieve user $userEmail5 related data.");
  446. $this->assertEquals(array_keys($user->getLists()), array($listId3), 'Fetched user lists does not match the one that was not removed.');
  447. $this->assertCallcount();
  448. return array(
  449. $userEmail => array('id' => $userId, 'listId' => $listId, 'status' => 0),
  450. $userEmail2 => array('id' => $userId2, 'listId' => $listId, 'status' => 1),
  451. );
  452. }
  453. /**
  454. * @covers Mailin\API::saveUser
  455. * @covers Mailin\API::getUser
  456. * @depends testAddList
  457. * @depends testAddAttributes
  458. */
  459. public function testSaveUserWithAttributes(array $foldersLists, array $attributes) {
  460. $listId = key(reset($foldersLists));
  461. $userEmail = \MailinTestHelper::randomEmail();
  462. $userAttributes = array();
  463. foreach ($attributes as $attributeType => $attributeInstances) {
  464. foreach ($attributeInstances as $attribute) {
  465. switch ($attributeType) {
  466. case MA\AttributeInterface::ATTRIBUTE_LIST_NORMAL:
  467. switch ($attribute->getDataType()) {
  468. case MA\AttributeInterface::ATTRIBUTE_DATA_TYPE_TEXT:
  469. $userAttributes[$attribute->getName()] = \MailinTestHelper::randomName();
  470. break;
  471. case MA\AttributeInterface::ATTRIBUTE_DATA_TYPE_NUMBER:
  472. $userAttributes[$attribute->getName()] = rand(1, 1000) . '.' . rand(1, 9);
  473. break;
  474. case MA\AttributeInterface::ATTRIBUTE_DATA_TYPE_DATE:
  475. $timestamp = rand(0, 1451602799); // january 1st, 1970 to december 31, 2015
  476. $userAttributes[$attribute->getName()] = date('Y-m-d', $timestamp);
  477. break;
  478. }//end switch
  479. break;
  480. case MA\AttributeInterface::ATTRIBUTE_LIST_CATEGORY:
  481. $enumeration = $attribute->getEnumeration();
  482. $key = array_rand($enumeration);
  483. $userAttributes[$attribute->getName()] = $enumeration[$key]['value'];
  484. break;
  485. }//end switch
  486. }//end foreach
  487. }//end foreach
  488. // Tests on user creation.
  489. $userId = $this->mailin->saveUser($userEmail, array($listId), $userAttributes);
  490. $this->assertTrue(is_numeric($userId), "Creation of user $userEmail failed.");
  491. $user = $this->mailin->getUser($userEmail);
  492. $this->assertTrue(is_object($user) && $user instanceof User, "Impossible to retrieve user $userEmail related data.");
  493. $this->assertCallcount(2);
  494. // Tests on user attributes.
  495. foreach ($attributes as $attributeType => $attributeInstances) {
  496. foreach ($attributeInstances as $attribute) {
  497. if (array_key_exists($attribute->getName(), $userAttributes)) {
  498. // Do not forget that User attribute types are not always the same as
  499. // Attribute types.
  500. $map = call_user_func(array(get_class($attribute), 'attributeEntityMap'));
  501. if (isset($map['user'])) {
  502. $handler = $user->getAttribute($map['user'], $attribute->getName());
  503. if (!isset($handler)) {
  504. $this->fail("The attribute {$attribute->getName()} was not saved.");
  505. }
  506. else {
  507. $this->assertEquals($handler->getValue(), $userAttributes[$attribute->getName()], "Saved data for {$attribute->getName()} attribute does not match the expected one: {$userAttributes[$attribute->getName()]}.");
  508. }
  509. }
  510. }
  511. }//end foreach
  512. }//end foreach
  513. }
  514. /**
  515. * @covers Mailin\API::saveUsers
  516. */
  517. public function testSaveUsers() {
  518. $this->markTestIncomplete();
  519. }
  520. /**
  521. * @covers Mailin\API::getUserStatus
  522. * @depends testSaveUser
  523. */
  524. public function testGetUserStatus(array $users) {
  525. $expected = array_map(function ($user) { return $user['status']; }, $users);
  526. $results = $this->mailin->getUserStatus(array_keys($users));
  527. ksort($expected);
  528. ksort($results);
  529. $this->assertInternalType('array', $results, "Users status retrieve operation failed.");
  530. $this->assertEquals($results, $expected, 'Retrieved results do not match expected values.');
  531. $this->assertCallcount();
  532. }
  533. /**
  534. * @covers Mailin\API::blockUser
  535. * @depends testSaveUser
  536. */
  537. public function testBlockUser(array $users) {
  538. list($userEmail, ) = each($users);
  539. $this->assertTrue($this->mailin->blockUser($userEmail), "The user $userEmail was blacklisted.");
  540. $this->assertEquals($this->mailin->getUserStatus(array($userEmail)), array($userEmail => 1), 'The user status is wrong.');
  541. $this->assertCallcount(2);
  542. }
  543. /**
  544. * @covers Mailin\API::unblockUser
  545. * @depends testSaveUser
  546. * @depends testBlockUser
  547. */
  548. public function testUnblockUser(array $users) {
  549. list($userEmail, ) = each($users);
  550. $this->assertTrue($this->mailin->unblockUser($userEmail), "The user $userEmail was removed from blacklist.");
  551. $this->assertEquals($this->mailin->getUserStatus(array($userEmail)), array($userEmail => 0), 'The user status is wrong.');
  552. $this->assertCallcount(2);
  553. }
  554. /**
  555. * @covers Mailin\API::deleteList
  556. * @depends testAddList
  557. * @depends testFindList
  558. *
  559. * Here we try to delete the lists that belongs to the first created folder,
  560. * to leave the testDeleteFolder method the possibility to delete the second
  561. * folder with its lists.
  562. */
  563. public function testDeleteList(array $foldersLists) {
  564. list($folderId, ) = array_keys($foldersLists);
  565. foreach ($foldersLists[$folderId] as $listId => $listName) {
  566. $this->assertTrue($this->mailin->deleteList($listId), "List $listName with ID $listId was not deleted.");
  567. $this->assertCallcount();
  568. // Verify that the list has really been removed from the Mailin server.
  569. $this->assertFalse($this->mailin->findList($listId), "List $listName with ID $listId was not removed.");
  570. $this->assertCallcount();
  571. // Try now to delete a non-existing list.
  572. $this->assertFalse($this->mailin->deleteList($listId), "List with unexisting ID $listId could not be deleted.");
  573. $this->assertCallcount();
  574. }//end foreach
  575. $foldersLists[$folderId] = array();
  576. return $foldersLists;
  577. }
  578. /**
  579. * @covers Mailin\API::testDeleteFolder
  580. * @depends testAddFolder
  581. * @depends testDeleteList
  582. * @depends testFindFolder
  583. */
  584. public function testDeleteFolder(array $folders, array $foldersLists) {
  585. foreach ($folders as $folderId => $folderName) {
  586. $this->assertTrue($this->mailin->deleteFolder($folderId), "Folder $folderName with ID $folderId was not deleted.");
  587. $this->assertCallcount();
  588. // Verify that the folder has really been removed from the Mailin server.
  589. $this->assertFalse($this->mailin->findFolder($folderId), "Folder $folderName with ID $folderId was not removed.");
  590. $this->assertCallcount();
  591. // Try now to delete a non-existing folder.
  592. $this->assertFalse($this->mailin->deleteFolder($folderId), "Folder with unexisting ID $folderId could not deleted.");
  593. $this->assertCallcount();
  594. // Ensure that child lists have also been deleted.
  595. if (!empty($foldersLists[$folderId])) {
  596. foreach ($foldersLists[$folderId] as $listId => $listName) {
  597. $this->assertFalse($this->mailin->findList($listId), "List $listName with ID $listId was not removed while deleting its parent folder.");
  598. $this->assertCallcount();
  599. }//end foreach
  600. }
  601. }//end foreach
  602. }
  603. }