Skip to content

Commit

Permalink
Fixing failing API-Library test for editing devices by PUT with ID th…
Browse files Browse the repository at this point in the history
…at does not work

TypeError: method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given
  • Loading branch information
escopecz committed Jan 16, 2023
1 parent 02c7973 commit e5d85be
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ protected function badRequest($msg = 'mautic.core.error.badrequest')
*/
protected function checkEntityAccess($entity, $action = 'view')
{
if ('create' != $action && method_exists($entity, 'getCreatedBy')) {
if ('create' !== $action && is_object($entity) && method_exists($entity, 'getCreatedBy')) {
$ownPerm = "{$this->permissionBase}:{$action}own";
$otherPerm = "{$this->permissionBase}:{$action}other";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Mautic\LeadBundle\Tests\Controller\Api;

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Entity\Lead;
use PHPUnit\Framework\Assert;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class DeviceApiControllerFunctionalTest extends MauticMysqlTestCase
{
public function testPutEditWithInexistingIdSoItShouldCreate(): void
{
$contact = new Lead();
$this->em->persist($contact);
$this->em->flush();

$this->client->request(Request::METHOD_PUT, '/api/devices/99999/edit', [
'device' => 'desktop',
'deviceOsName' => 'Ubuntu',
'deviceOsShortName' => 'UBT',
'deviceOsPlatform' => 'x64',
'lead' => $contact->getId(),
]);

$clientResponse = $this->client->getResponse();

Assert::assertSame(Response::HTTP_CREATED, $clientResponse->getStatusCode());
}
}

0 comments on commit e5d85be

Please sign in to comment.