<?php
// $Id: flickrrippr.test,v 1.4.2.2 2010/11/16 01:24:18 taniwha Exp $

/**
 * @file
 * Unit Tests for flickrrippr module
 * http://drupal.org/node/265762
 */

class FlickrRipprTestCase extends DrupalWebTestCase {

  /**
   * User with rights to post SimpleTest Example content.
   */
  protected $admin_user;
  protected $normal_user;
  protected $apikey = '1611f2ad417e2276c05fa279654cc0e8';
  protected $flickrusername = 'Flickr Rippr';
  protected $nsid = '54551252@N03';

  /**
   * getInfo() returns properties that are displayed in the test selection form.
   */
  public static function getInfo() {
    return array(
      'name' => 'Flickr Rippr',
      'description' => 'Ensure that the flickrrippr content type provided functions properly.',
      'group' => 'Flickr Rippr',
    );
  }

  /**
   * setUp() performs any pre-requisite tasks that need to happen.
   */
  public function setUp() {
    // Enable any modules required for the test.
    parent::setUp('flickrrippr', 'flickrapi', 'job_queue');

    $this->admin_user = $this->drupalCreateUser( array('Administer global flickr api settings', 'administer site configuration'));
    $this->normal_user = $this->drupalCreateUser( array('Configure own blogged flickr stream'));
    // Create and log in our privileged user.
  }

  function testConfiguringModule() {

    //log in as admin
    $this->drupalLogin($this->admin_user);
//    $this->verbose(print_r($this->admin_user, 1));

    //configure the flickr api module

    $this->drupalGet('admin/settings/flickrapi');
    $this->assertResponse(200);
    $this->assertText('API Key');
    $form['flickrapi_api_key'] = $this->apikey;
    $form['flickrapi_cache_duration'] = 900;
    $this->drupalPost('admin/settings/flickrapi', $form, 'Save configuration');
    $this->assertResponse(200);


    //configure flickrrippr
    $this->drupalGet('admin/flickr/flickrrippr');
    $form = array();
    $form['path_handler'] = 0;
    $form['markup_trust'] = FALSE;
    $form['num_per_fetch'] = 5;
    $form['teaser_size'] = 's';
    $form['page_size'] = 'b';
    $this->drupalPost('admin/flickr/flickrrippr', $form, 'Save');

    //re-read the variables that just got saved;
    $this->refreshVariables();

    $this->drupalLogout();



    //log in as a normal user
    $this->drupalLogin($this->normal_user);

    //set their account details
    $this->clickLink('My account');
    $this->assertLink('Flickr Rippr');
    $this->clickLink('Flickr Rippr');
    $this->assertNoText('Flickr ID', 'Table should not display when there are no accounts set up yet');

    $this->clickLink('Add new account');
    $this->assertText('add');
    $this->assertText('Flickr Username');
    $this->assertText('Filter by Tag');

    $edit['flickr_username'] = strtolower($this->flickrusername);
    $edit['flickr_tag'] = 'fruit';
    $this->drupalPost($this->getUrl(), $edit, 'Save');

    //check it all saved
    $this->assertText($this->flickrusername); //it'll have been capitalised via the API
    $this->assertText($this->nsid);
    $this->assertText('fruit');

    //check the image appeared
    $this->assertRaw('http://farm5.static.flickr.com/4105/buddyicons/54551252@N03.jpg');

    //do a fetch of this account
    $this->drupalPost($this->getUrl(), array(), 'Fetch now');

    //TODO edit, and check the details appear
    $this->clickLink('Edit', 1); //the 2nd Edit link, not the edit in the tabs
    $this->assertNoText('add');
    $this->assertText('Flickr Username');
    $this->assertText('Filter by Tag');
    $form = array('flickr_tag' => '');
    $this->drupalPost($this->geturl(), $form, 'Save');

    //add a kitty
    $this->assertNoText('fruit');
    $this->clickLink('Edit', 1);
    //try adding the same account again, must fail

    //TODO try to configure module, they shouldn't be able to.
    $this->drupalGet('admin/content/flickrrippr');
    $this->assertResponse(403);
    $this->drupalGet('admin/flickr/flickrrippr');
    $this->assertResponse(403);

    $this->drupalLogout();

    //log in as admin again
    $this->drupalLogin($this->admin_user);
    //set go to admin/content/flickrrippr
    $this->drupalGet('admin/content/flickrrippr');
    //TODO check the account created above are there
    //TODO  fetch photos for some accounts

    $this->drupalLogout();

    //run cron
    //$this->cronRun();
    $this->drupalGet('cron.php');

    //TODO Check that some photos got imported
  }


}
