|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the bitrix24-php-sdk package. |
| 5 | + * |
| 6 | + * © Maksim Mesilov <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the MIT-LICENSE.txt |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Bitrix24\SDK\Tests\Integration\Services\CRM\Item\Service; |
| 15 | + |
| 16 | +use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; |
| 17 | +use Bitrix24\SDK\Core\Exceptions\ItemNotFoundException; |
| 18 | +use Bitrix24\SDK\Services\CRM\Contact\Service\Contact; |
| 19 | +use Bitrix24\SDK\Services\CRM\Item\Service\Item; |
| 20 | +use Bitrix24\SDK\Services\CRM\Type\Service\Type; |
| 21 | +use Bitrix24\SDK\Tests\Integration\Fabric; |
| 22 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | +use Bitrix24\SDK\Tests\CustomAssertions\CustomBitrix24Assertions; |
| 25 | + |
| 26 | +#[CoversClass(Item::class)] |
| 27 | +class ItemTest extends TestCase |
| 28 | +{ |
| 29 | + use CustomBitrix24Assertions; |
| 30 | + |
| 31 | + protected Type $typeService; |
| 32 | + |
| 33 | + protected Item $itemService; |
| 34 | + |
| 35 | + protected Contact $contactService; |
| 36 | + |
| 37 | + public function testAdd(): void |
| 38 | + { |
| 39 | + $title = sprintf('%s test SPA type', time()); |
| 40 | + $addedTypeItemResult = $this->typeService->add($title); |
| 41 | + $this->assertEquals($title, $addedTypeItemResult->type()->title); |
| 42 | + |
| 43 | + // add item to SP |
| 44 | + $this->itemService->add($addedTypeItemResult->type()->entityTypeId, [ |
| 45 | + 'title' => sprintf('test sp item %s', time()), |
| 46 | + 'xmlId' => sprintf('b24-php-sdk-test-item-%s', time()) |
| 47 | + ])->item(); |
| 48 | + |
| 49 | + $this->assertTrue($this->typeService->delete($addedTypeItemResult->getId())->isSuccess()); |
| 50 | + } |
| 51 | + |
| 52 | + public function testUpdate(): void |
| 53 | + { |
| 54 | + $title = sprintf('%s test SPA type', time()); |
| 55 | + $addedTypeItemResult = $this->typeService->add($title); |
| 56 | + $this->assertEquals($title, $addedTypeItemResult->type()->title); |
| 57 | + |
| 58 | + // add item to SP |
| 59 | + $itemItemResult = $this->itemService->add($addedTypeItemResult->type()->entityTypeId, [ |
| 60 | + 'title' => sprintf('test sp item %s', time()), |
| 61 | + 'xmlId' => sprintf('b24-php-sdk-test-item-%s', time()) |
| 62 | + ])->item(); |
| 63 | + |
| 64 | + // update item |
| 65 | + $newTitle = sprintf('test sp item %s updated', time()); |
| 66 | + $updatedItemResult = $this->itemService->update($addedTypeItemResult->type()->entityTypeId, $itemItemResult->id, ['title' => $newTitle]); |
| 67 | + $this->assertEquals($newTitle, $updatedItemResult->item()->title); |
| 68 | + $this->assertTrue($updatedItemResult->isSuccess()); |
| 69 | + |
| 70 | + $updatedItem = $this->itemService->get($addedTypeItemResult->type()->entityTypeId, $itemItemResult->id)->item(); |
| 71 | + $this->assertEquals($newTitle, $updatedItem->title); |
| 72 | + |
| 73 | + $this->assertTrue($this->typeService->delete($addedTypeItemResult->getId())->isSuccess()); |
| 74 | + } |
| 75 | + |
| 76 | + public function testGet(): void |
| 77 | + { |
| 78 | + $title = sprintf('%s test SPA type', time()); |
| 79 | + $addedTypeItemResult = $this->typeService->add($title); |
| 80 | + $this->assertEquals($title, $addedTypeItemResult->type()->title); |
| 81 | + |
| 82 | + // add item to SP |
| 83 | + $itemItemResult = $this->itemService->add($addedTypeItemResult->type()->entityTypeId, [ |
| 84 | + 'title' => sprintf('test sp item %s', time()), |
| 85 | + 'xmlId' => sprintf('b24-php-sdk-test-item-%s', time()) |
| 86 | + ])->item(); |
| 87 | + |
| 88 | + $item = $this->itemService->get($addedTypeItemResult->type()->entityTypeId, $itemItemResult->id)->item(); |
| 89 | + $this->assertEquals($itemItemResult->title, $item->title); |
| 90 | + $this->assertEquals($itemItemResult->xmlId, $item->xmlId); |
| 91 | + |
| 92 | + $this->assertTrue($this->typeService->delete($addedTypeItemResult->type()->entityTypeId)->isSuccess()); |
| 93 | + } |
| 94 | + |
| 95 | + public function testList(): void |
| 96 | + { |
| 97 | + $title = sprintf('%s test SPA type', time()); |
| 98 | + $addedTypeItemResult = $this->typeService->add($title); |
| 99 | + $this->assertEquals($title, $addedTypeItemResult->type()->title); |
| 100 | + |
| 101 | + // add item to SP |
| 102 | + $this->itemService->add($addedTypeItemResult->type()->entityTypeId, [ |
| 103 | + 'title' => sprintf('test sp item %s', time()), |
| 104 | + 'xmlId' => sprintf('b24-php-sdk-test-item-%s', time()) |
| 105 | + ])->item(); |
| 106 | + $this->itemService->add($addedTypeItemResult->type()->entityTypeId, [ |
| 107 | + 'title' => sprintf('test sp item %s', time()), |
| 108 | + 'xmlId' => sprintf('b24-php-sdk-test-item-%s', time()) |
| 109 | + ])->item(); |
| 110 | + |
| 111 | + $items = $this->itemService->list($addedTypeItemResult->type()->entityTypeId, [], [], [])->getItems(); |
| 112 | + $this->assertCount(2, $items); |
| 113 | + |
| 114 | + $this->assertTrue($this->typeService->delete($addedTypeItemResult->type()->entityTypeId)->isSuccess()); |
| 115 | + } |
| 116 | + |
| 117 | + public function testGetSmartProcessItem(): void |
| 118 | + { |
| 119 | + $title = sprintf('%s test SPA type', time()); |
| 120 | + $addedTypeItemResult = $this->typeService->add($title, null, [ |
| 121 | + 'relations' => [ |
| 122 | + 'child' => [ |
| 123 | + [ |
| 124 | + // allow bind to contact |
| 125 | + 'entityTypeId' => 3, |
| 126 | + 'isChildrenListEnabled' => 'N', |
| 127 | + 'isPredefined' => 'N' |
| 128 | + ] |
| 129 | + ] |
| 130 | + ] |
| 131 | + ]); |
| 132 | + $this->assertEquals($title, $addedTypeItemResult->type()->title); |
| 133 | + |
| 134 | + // add item to SP |
| 135 | + $itemItemResult = $this->itemService->add($addedTypeItemResult->type()->entityTypeId, [ |
| 136 | + 'title' => sprintf('test sp item %s', time()), |
| 137 | + 'xmlId' => sprintf('b24-php-sdk-test-item-%s', time()) |
| 138 | + ])->item(); |
| 139 | + |
| 140 | + // add contact with sp item |
| 141 | + // @phpstan-ignore-next-line argument.type |
| 142 | + $b24ContactId = $this->contactService->add([ |
| 143 | + 'NAME' => sprintf('Test contact %s', time()), |
| 144 | + 'PARENT_ID_' . $addedTypeItemResult->type()->entityTypeId => $itemItemResult->id, |
| 145 | + ])->getId(); |
| 146 | + $contact = $this->contactService->get($b24ContactId)->contact(); |
| 147 | + $this->assertEquals( |
| 148 | + $itemItemResult->id, |
| 149 | + $contact->getSmartProcessItem($addedTypeItemResult->type()->entityTypeId) |
| 150 | + ); |
| 151 | + $this->expectException(InvalidArgumentException::class); |
| 152 | + $contact->getSmartProcessItem(1); |
| 153 | + |
| 154 | + $b24ContactId = $this->contactService->add([ |
| 155 | + 'NAME' => sprintf('Test contact %s', time()) |
| 156 | + ])->getId(); |
| 157 | + $this->assertNull($this->contactService->get($b24ContactId)->contact()->getSmartProcessItem($addedTypeItemResult->type()->entityTypeId)); |
| 158 | + |
| 159 | + $this->assertTrue($this->typeService->delete($addedTypeItemResult->type()->entityTypeId)->isSuccess()); |
| 160 | + } |
| 161 | + |
| 162 | + public function testDelete(): void |
| 163 | + { |
| 164 | + $title = sprintf('%s test SPA type', time()); |
| 165 | + $addedTypeItemResult = $this->typeService->add($title); |
| 166 | + $this->assertEquals($title, $addedTypeItemResult->type()->title); |
| 167 | + |
| 168 | + // add item to SP |
| 169 | + $itemItemResult = $this->itemService->add($addedTypeItemResult->type()->entityTypeId, [ |
| 170 | + 'title' => sprintf('test sp item %s', time()), |
| 171 | + 'xmlId' => sprintf('b24-php-sdk-test-item-%s', time()) |
| 172 | + ])->item(); |
| 173 | + |
| 174 | + $this->assertTrue($this->itemService->delete($addedTypeItemResult->type()->entityTypeId, $itemItemResult->id)->isSuccess()); |
| 175 | + |
| 176 | + $this->expectException(ItemNotFoundException::class); |
| 177 | + $this->itemService->get($addedTypeItemResult->type()->entityTypeId, $itemItemResult->id)->item(); |
| 178 | + |
| 179 | + $this->assertTrue($this->typeService->delete($addedTypeItemResult->type()->entityTypeId)->isSuccess()); |
| 180 | + } |
| 181 | + |
| 182 | + protected function setUp(): void |
| 183 | + { |
| 184 | + $this->typeService = Fabric::getServiceBuilder()->getCRMScope()->type(); |
| 185 | + $this->itemService = Fabric::getServiceBuilder()->getCRMScope()->item(); |
| 186 | + $this->contactService = Fabric::getServiceBuilder()->getCRMScope()->contact(); |
| 187 | + } |
| 188 | +} |
0 commit comments