CakePHPのアプリをSeleniumでテストする。


# Target

- [Bug #522 Deleting of related issue fails - CandyCane's CandyCane - CandyCane on Engine Yard Cloud](http://my.candycane.jp/issues/show/522)
- https://github.com/yandod/candycane/commit/f9efce005b64b4ce25dc17d8214ade32018a5ca4#diff-d41d8cd98f00b204e9800998ecf8427e

## Prepare

```
vagrant ssh
cd /vagrant_data/
/usr/bin/Xvfb :1 -screen 0 1024x768x8 > /tmp/xvfb.log 2> /tmp/xvfb.error &
export DISPLAY=:1.0
java -jar /var/chef/cache/selenium-server-standalone-2.39.0.jar > /tmp/selenium.log 2> /tmp/selenium.error &
mysql -u root -e "drop database if exists test_candycane;create database test_candycane;"
./vendor/bin/phpunit app/Test/Case/Selenium/InstallerTest.php
```

## First Step

```
<?php
/**
 * Created by PhpStorm.
 * User: yandod
 * Date: 2014/08/22
 * Time: 11:01
 */

class IssueTest extends PHPUnit_Extensions_Selenium2TestCase {

    protected function setUp()
    {
        $this->setHost('127.0.0.1');
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://127.0.0.1/');
        //$this->setPort(80);
    }

    public function testCreateShowDestroy()
    {

        $this->url('http://127.0.0.1/projects/sampleproject');
        $this->assertEquals('Overview - Sample Project - CandyCane', $this->title());
        $link = $this->byLinkText('New Issue');
    }

    public function onNotSuccessfulTest(Exception $e)
    {
        file_put_contents('app/tmp/logs/screenshot.png',$this->currentScreenshot());
		throw $e;
    }
} 
```

```
./vendor/bin/phpunit app/Test/Case/Selenium/IssueTest.php
```

![screenshot.png](https://qiita-image-store.s3.amazonaws.com/0/10032/c5dd50c0-4e93-f4f9-fb59-d5742b806b70.png "screenshot.png")

## Login

```
  public function testCreateShowDestroy()
    {

        //login
        $this->url('http://127.0.0.1/account/login');
        $input = $this->byName('data[User][username]');
        $input->clear();
        $input->value('admin');
        $input = $this->byName('data[User][password]');
        $input->clear();
        $input->value('admin');
        $button = $this->byName('login');
        $this->moveto($button);
        $this->byId('UserLoginForm')->submit();

        //navigate
        $this->url('http://127.0.0.1/projects/sampleproject');
        $this->assertEquals('Overview - Sample Project - CandyCane', $this->title());
        $link = $this->byLinkText('New issue');
        $this->moveto($link);
        $this->click();

        //new issue
        $this->assertEquals('e', $this->title());
        file_put_contents('app/tmp/logs/screenshot.png',$this->currentScreenshot());
    }
```

![screenshot.png](https://qiita-image-store.s3.amazonaws.com/0/10032/22b66a1f-c24a-ddc0-cc77-2af16bb502b7.png "screenshot.png")

## Create Issue

```
    public function testCreateShowDestroy()
    {

        //login
        $this->url('http://127.0.0.1/account/login');
        $input = $this->byName('data[User][username]');
        $input->clear();
        $input->value('admin');
        $input = $this->byName('data[User][password]');
        $input->clear();
        $input->value('admin');
        $button = $this->byName('login');
        $this->moveto($button);
        $this->byId('UserLoginForm')->submit();

        //navigate
        $this->url('http://127.0.0.1/projects/sampleproject');
        $this->assertEquals('Overview - Sample Project - CandyCane', $this->title());
        $link = $this->byLinkText('New issue');
        $this->moveto($link);
        $this->click();

        //new issue
        $input = $this->byId('IssueSubject');
        $input->value('We are in Spain');
        $input = $this->byId('description');
        $input->value('Hola.');
        $this->byId('IssueAddForm')->submit();

        file_put_contents('app/tmp/logs/screenshot.png',$this->currentScreenshot());
    }
```

![screenshot.png](https://qiita-image-store.s3.amazonaws.com/0/10032/0adc16cc-b745-76b7-ce8a-48578f26f466.png "screenshot.png")

## Add Relate Issue

```
    public function testCreateShowDestroy()
    {

        //login
        $this->url('http://127.0.0.1/account/login');
        $input = $this->byName('data[User][username]');
        $input->clear();
        $input->value('admin');
        $input = $this->byName('data[User][password]');
        $input->clear();
        $input->value('admin');
        $button = $this->byName('login');
        $this->moveto($button);
        $this->byId('UserLoginForm')->submit();

        //navigate
        $this->url('http://127.0.0.1/projects/sampleproject');
        $this->assertEquals('Overview - Sample Project - CandyCane', $this->title());
        $link = $this->byLinkText('New issue');
        $this->moveto($link);
        $this->click();

        //new issue
        $input = $this->byId('IssueSubject');
        $input->value('We are in Spain');
        $input = $this->byId('description');
        $input->value('Hola.');
        $this->byId('IssueAddForm')->submit();

        //add more issue
        $link = $this->byLinkText('New issue');
        $this->moveto($link);
        $this->click();
        $input = $this->byId('IssueSubject');
        $input->value('We are in Madrid');
        $input = $this->byId('description');
        $input->value('Madrid is center of Spain.');
        $this->byId('IssueAddForm')->submit();

        //link issue
        $link = $this->byLinkText('Add');
        $this->moveto($link);
        $this->click();
        $input = $this->byId('IssueRelationIssueToId');
        $current_id = array_pop(explode('/',$this->url())) - 1;
        $input->value($current_id);
        $this->byId('new-relation-form')->submit();
        $this->timeouts()->implicitWait(800);

        file_put_contents('app/tmp/logs/screenshot.png',$this->currentScreenshot());
    }
```

![screenshot.png](https://qiita-image-store.s3.amazonaws.com/0/10032/57c3245c-ebf8-7584-1887-2b5492e43d4f.png "screenshot.png")

## Click delete relation.

XPath!

```
    public function testCreateShowDestroy()
    {

        //login
        $this->url('http://127.0.0.1/account/login');
        $input = $this->byName('data[User][username]');
        $input->clear();
        $input->value('admin');
        $input = $this->byName('data[User][password]');
        $input->clear();
        $input->value('admin');
        $button = $this->byName('login');
        $this->moveto($button);
        $this->byId('UserLoginForm')->submit();

        //navigate
        $this->url('http://127.0.0.1/projects/sampleproject');
        $this->assertEquals('Overview - Sample Project - CandyCane', $this->title());
        $link = $this->byLinkText('New issue');
        $this->moveto($link);
        $this->click();
        $this->timeouts()->implicitWait(300);

        //new issue
        $input = $this->byId('IssueSubject');
        $input->value('We are in Spain');
        $input = $this->byId('description');
        $input->value('Hola.');
        $this->byId('IssueAddForm')->submit();

        //add more issue
        $link = $this->byLinkText('New issue');
        $this->moveto($link);
        $this->click();
        $input = $this->byId('IssueSubject');
        $input->value('We are in Madrid');
        $input = $this->byId('description');
        $input->value('Madrid is center of Spain.');
        $this->byId('IssueAddForm')->submit();

        //link issue
        $link = $this->byLinkText('Add');
        $this->moveto($link);
        $this->click();
        $input = $this->byId('IssueRelationIssueToId');
        $current_id = array_pop(explode('/',$this->url())) - 1;
        $input->value($current_id);
        $this->byId('new-relation-form')->submit();
        $this->timeouts()->implicitWait(800);

        //delete relation
        $link = $this->byXPath("//a[@title='Delete relation']");
        $this->moveto($link);
        $this->click();
        $this->timeouts()->implicitWait(300);

        file_put_contents('app/tmp/logs/screenshot.png',$this->currentScreenshot());
    }
```

![screenshot.png](https://qiita-image-store.s3.amazonaws.com/0/10032/06effb64-2dfb-e9e7-2cde-f27c45c267c5.png "screenshot.png")

## Bug Fix!

```php:IssueReleationsController.php
    function destroy($relation_id)
    {
        if ($this->request->is('ajax')) {
            Configure::write('debug', 0);
        }

        $relation = $this->IssueRelation->read(null, $relation_id);
        if ($relation) {
            $this->IssueRelation->delete();
        }

        $this->redirect(array('controller' => 'issues', 'action' => 'show', $this->_issue['Issue']['id']));
    }
```

```php:View/Elements/issues/relations.ctp
  <?php if($this->Candy->authorize_for(array('controller'=>'issue_relations', 'action'=>'destroy'))) {
    echo $this->Js->link($this->Html->image('delete.png'), 
      array('controller'=>'issue_relations', 'action'=>'destroy', 'issue_id'=>$issue['Issue']['id'], $relation['IssueRelation']['id']),
      array('method'=>'post', 'title'=> __('Delete relation'), 'update'=>'relations', 'escape' => false));
  }?>
```

![screenshot.png](https://qiita-image-store.s3.amazonaws.com/0/10032/1360e433-e08b-2a5d-3a34-bcdef1337a63.png "screenshot.png")