<commit_before><?php
/**
 * Pterodactyl - Panel
 * Copyright (c) 2015 - 2017 <NAME> <<EMAIL>>.
 *
 * This software is licensed under the terms of the MIT license.
 * https://opensource.org/licenses/MIT
 */

namespace Tests\Unit\Services\Nodes;

use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Node;
use Illuminate\Contracts\Translation\Translator;
use Pterodactyl\Services\Nodes\NodeDeletionService;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;

class NodeDeletionServiceTest extends TestCase
{
    /**
     * @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
     */
    protected $repository;

    /**
     * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
     */
    protected $serverRepository;

    /**
     * @var \Illuminate\Contracts\Translation\Translator
     */
    protected $translator;

    /**
     * @var \Pterodactyl\Services\Nodes\NodeDeletionService
     */
    protected $service;

    /**
     * Setup tests.
     */
    public function setUp()
    {
        parent::setUp();

        $this->repository = m::mock(NodeRepositoryInterface::class);
        $this->serverRepository = m::mock(ServerRepositoryInterface::class);
        $this->translator = m::mock(Translator::class);

        $this->service = new NodeDeletionService(
            $this->repository,
            $this->serverRepository,
            $this->translator
        );
    }

    /**
     * Test that a node is deleted if there are no servers attached to it.
     */
    public function testNodeIsDeletedIfNoServersAreAttached()
    {
        $this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
            ->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(0);
        $this->repository->shouldReceive('delete')->with(1)->once()->andReturn(true);

        $this->assertTrue(
            $this->service->handle(1),
            'Assert that deletion returns a positive boolean value.'
        );
    }

    /**
     * Test that an exception is thrown if servers are attached to the node.
     *
     * @expectedException \Pterodactyl\Exceptions\DisplayException
     */
    public function testExceptionIsThrownIfServersAreAttachedToNode()
    {
        $this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
            ->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(1);
        $this->translator->shouldReceive('trans')->with('exceptions.node.servers_attached')->once()->andReturnNull();
        $this->repository->shouldNotReceive('delete');

        $this->service->handle(1);
    }

    /**
     * Test that a model can be passed into the handle function rather than an ID.
     */
    public function testModelCanBePassedToFunctionInPlaceOfNodeId()
    {
        $node = factory(Node::class)->make();

        $this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
            ->shouldReceive('findCountWhere')->with([['node_id', '=', $node->id]])->once()->andReturn(0);
        $this->repository->shouldReceive('delete')->with($node->id)->once()->andReturn(true);

        $this->assertTrue(
            $this->service->handle($node->id),
            'Assert that deletion returns a positive boolean value.'
        );
    }
}
<commit_msg>Pass in what the test says it passes in.<commit_after><?php
/**
 * Pterodactyl - Panel
 * Copyright (c) 2015 - 2017 <NAME> <<EMAIL>>.
 *
 * This software is licensed under the terms of the MIT license.
 * https://opensource.org/licenses/MIT
 */

namespace Tests\Unit\Services\Nodes;

use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Node;
use Illuminate\Contracts\Translation\Translator;
use Pterodactyl\Services\Nodes\NodeDeletionService;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;

class NodeDeletionServiceTest extends TestCase
{
    /**
     * @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
     */
    protected $repository;

    /**
     * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
     */
    protected $serverRepository;

    /**
     * @var \Illuminate\Contracts\Translation\Translator
     */
    protected $translator;

    /**
     * @var \Pterodactyl\Services\Nodes\NodeDeletionService
     */
    protected $service;

    /**
     * Setup tests.
     */
    public function setUp()
    {
        parent::setUp();

        $this->repository = m::mock(NodeRepositoryInterface::class);
        $this->serverRepository = m::mock(ServerRepositoryInterface::class);
        $this->translator = m::mock(Translator::class);

        $this->service = new NodeDeletionService(
            $this->repository,
            $this->serverRepository,
            $this->translator
        );
    }

    /**
     * Test that a node is deleted if there are no servers attached to it.
     */
    public function testNodeIsDeletedIfNoServersAreAttached()
    {
        $this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
            ->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(0);
        $this->repository->shouldReceive('delete')->with(1)->once()->andReturn(true);

        $this->assertTrue(
            $this->service->handle(1),
            'Assert that deletion returns a positive boolean value.'
        );
    }

    /**
     * Test that an exception is thrown if servers are attached to the node.
     *
     * @expectedException \Pterodactyl\Exceptions\DisplayException
     */
    public function testExceptionIsThrownIfServersAreAttachedToNode()
    {
        $this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
            ->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(1);
        $this->translator->shouldReceive('trans')->with('exceptions.node.servers_attached')->once()->andReturnNull();
        $this->repository->shouldNotReceive('delete');

        $this->service->handle(1);
    }

    /**
     * Test that a model can be passed into the handle function rather than an ID.
     */
    public function testModelCanBePassedToFunctionInPlaceOfNodeId()
    {
        $node = factory(Node::class)->make();

        $this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
            ->shouldReceive('findCountWhere')->with([['node_id', '=', $node->id]])->once()->andReturn(0);
        $this->repository->shouldReceive('delete')->with($node->id)->once()->andReturn(true);

        $this->assertTrue(
            $this->service->handle($node),
            'Assert that deletion returns a positive boolean value.'
        );
    }
}

{'fraction_non_alphanumeric': 0.15060153645455862, 'fraction_numerical': 0.0046383533845484855, 'mean_word_length': 3.09982174688057, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 134, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '411781', 'n_tokens_mistral': 1975, 'n_tokens_neox': 1920, 'n_words': 340}