<commit_before><?php

namespace SimplyTestable\WorkerBundle\Tests\Controller;

class TaskControllerCancelTest extends TaskControllerTest {
    
    const TASK_CONTROLLER_NAME = 'SimplyTestable\WorkerBundle\Controller\TaskController';
    
    public function setUp() {
        parent::setUp();
        $this->setupDatabase();
    }

    public function testCancelActionInNormalState() {
        $this->setActiveState();
        
        $url = 'http://example.com/';
        $type = 'HTML validation';        
        
        $createdTask = $this->createTask($url, $type);
        
        $postData = array(
            'id' => $createdTask->id
        );        
        
        $controller = $this->getTaskController('cancelAction', $postData);
       
        $cancellationResponse = $controller->cancelAction();        
        $cancelledTask = json_decode($cancellationResponse->getContent());
        
        $this->assertEquals(200, $cancellationResponse->getStatusCode());
        $this->assertEquals('cancelled', $cancelledTask->state);      
    }
    
    
    public function testCancelActionInMaintenanceReadOnlyStateReturnsHttp503() {                
        $this->setActiveState();
        
        $url = 'http://example.com/';
        $type = 'HTML validation';        
        
        $createdTask = $this->createTask($url, $type);
        
        $postData = array(
            'id' => $createdTask->id
        );  
        
        $this->setMaintenanceReadOnlyState();        
        
        $cancellationResponse =$this->getTaskController('cancelAction', $postData)->cancelAction();        
        $this->assertEquals(503, $cancellationResponse->getStatusCode());    
    }
    
    
    public function testCancelCollectionActionInNormalState() {
        $this->setActiveState();

        $taskData = array(
                array(
                    'url' => 'http://example.com/one/',
                    'type' => 'HTML validation'
                ),
                array(
                    'url' => 'http://example.com/two/',
                    'type' => 'CSS validation'
                ),
                array(
                    'url' => 'http://example.com/three/',
                    'type' => 'JS static analysis'
                ),              
        );        
        
        $tasks = $this->createTaskCollection($taskData);
        
        $taskIds = array();
        foreach ($tasks as $task) {
            $taskIds[] = $task->id;
        }
        
        $controller = $this->getTaskController('cancelCollectionAction', array(
            'ids' => implode(',', $taskIds)
        ));
       
        $cancellationResponse = $controller->cancelCollectionAction();      
        $this->assertEquals(200, $cancellationResponse->getStatusCode());          
    }
    
    
    public function testCancelCollectionActionInMaintenanceReadOnlyStateReturnsHttp503() {
        $this->setActiveState();

        $taskData = array(
                array(
                    'url' => 'http://example.com/one/',
                    'type' => 'HTML validation'
                ),
                array(
                    'url' => 'http://example.com/two/',
                    'type' => 'CSS validation'
                ),
                array(
                    'url' => 'http://example.com/three/',
                    'type' => 'JS static analysis'
                ),              
        );        
        
        $tasks = $this->createTaskCollection($taskData);
        
        $taskIds = array();
        foreach ($tasks as $task) {
            $taskIds[] = $task->id;
        }
        <commit_msg>TaskControllerCancelTest only rebuild db when relevant<commit_after><?php

namespace SimplyTestable\WorkerBundle\Tests\Controller;

class TaskControllerCancelTest extends TaskControllerTest {    
    
    public static function setUpBeforeClass() {
        self::setupDatabaseIfNotExists();        
    }

    public function testCancelActionInNormalState() {
        $url = 'http://example.com/';
        $type = 'HTML validation';        
        
        $createdTask = $this->createTask($url, $type);
        
        $postData = array(
            'id' => $createdTask->id
        );        
        
        $controller = $this->getTaskController('cancelAction', $postData);
       
        $cancellationResponse = $controller->cancelAction();        
        $cancelledTask = json_decode($cancellationResponse->getContent());
        
        $this->assertEquals(200, $cancellationResponse->getStatusCode());
        $this->assertEquals('cancelled', $cancelledTask->state);      
    }
    
    
    public function testCancelActionInMaintenanceReadOnlyStateReturnsHttp503() {
        $url = 'http://example.com/';
        $type = 'HTML validation';        
        
        $createdTask = $this->createTask($url, $type);
        
        $postData = array(
            'id' => $createdTask->id
        );  
        
        $this->setMaintenanceReadOnlyState();        
        
        $cancellationResponse =$this->getTaskController('cancelAction', $postData)->cancelAction();        
        $this->assertEquals(503, $cancellationResponse->getStatusCode());    
    }
    
    
    public function testCancelCollectionActionInNormalState() {
        $taskData = array(
                array(
                    'url' => 'http://example.com/one/',
                    'type' => 'HTML validation'
                ),
                array(
                    'url' => 'http://example.com/two/',
                    'type' => 'CSS validation'
                ),
                array(
                    'url' => 'http://example.com/three/',
                    'type' => 'JS static analysis'
                ),              
        );        
        
        $tasks = $this->createTaskCollection($taskData);
        
        $taskIds = array();
        foreach ($tasks as $task) {
            $taskIds[] = $task->id;
        }
        
        $controller = $this->getTaskController('cancelCollectionAction', array(
            'ids' => implode(',', $taskIds)
        ));
       
        $cancellationResponse = $controller->cancelCollectionAction();      
        $this->assertEquals(200, $cancellationResponse->getStatusCode());          
    }
    
    
    public function testCancelCollectionActionInMaintenanceReadOnlyStateReturnsHttp503() {
        $taskData = array(
                array(
                    'url' => 'http://example.com/one/',
                    'type' => 'HTML validation'
                ),
                array(
                    'url' => 'http://example.com/two/',
                    'type' => 'CSS validation'
                ),
                array(
                    'url' => 'http://example.com/three/',
                    'type' => 'JS static analysis'
                ),              
        );        
        
        $tasks = $this->createTaskCollection($taskData);
        
        $taskIds = array();
        foreach ($tasks as $task) {
            $taskIds[] = $task->id;
        }
        
{'fraction_non_alphanumeric': 0.1288102261553589, 'fraction_numerical': 0.004214075010535188, 'mean_word_length': 1.490381252186079, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 86, 'https://': 0, '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': '3439314', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1780, 'n_words': 286}