<?php

/*
 * This file is part of the sfLucenePlugin package
 * (c) 2007 - 2008 Carl Vondrick <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
/**
 * @package sfLucenePlugin
 * @subpackage Test
 * @author Carl Vondrick
 * @version SVN: $Id: sfLuceneLazyLoadingTest.php 7108 2008-01-20 07:44:42Z Carl.Vondrick $
 */
require dirname(__FILE__) . '/../bootstrap/unit.php';
$t = new limeade_test(2, limeade_output::get());
$limeade = new limeade_sf($t);
$limeade->bootstrap();
$luceneade = new limeade_lucene($limeade);
$luceneade->configure()->clear_sandbox()->load_models();
$forum = new FakeForum();
$t->not_like_included('#/Zend/Search/Lucene/#', 'Zend libraries were not loaded when just reading from a model');
$forum->setTitle('test');
$forum->saveIndex();
$t->like_included('#/Zend/Search/Lucene/#', 'Zend libraries were loaded when writing to the index');
$forum->deleteIndex();
$luceneade = new limeade_lucene($limeade);
$luceneade->configure()->clear_sandbox();
$t->diag('testing ::loadZend()');
sfConfig::set('app_lucene_zend_location', '/tmp');
try {
    $e = $t->exception('::loadZend() fails with non-existant Zend path');
    sfLuceneToolkit::loadZend();
    $e->no();
} catch (Exception $ex) {
    $e->caught($ex);
}
$limeade->config()->remove('app_lucene_zend_location');
$t->not_like_included('#/Zend/Search/Lucene/#', 'Zend Search Lucene is not loaded after failed run');
$t->not_in_include_path('Zend/Search/Lucene.php', 'Zend Search Lucene is not in the include path after failed run');
sfLuceneToolkit::loadZend();
$t->like_included('#/Zend/Search/Lucene/#', '::loadZend() loads Zend Search Lucene');
$t->in_include_path('Zend/Search/Lucene.php', '::loadZend() configures include path');
$t->diag('testing ::getDirtyIndexRemains()');
$luceneade->clear_sandbox();
$root = sfConfig::get('sf_data_dir') . '/index/';
// build valid indexes structure
sfLucene::getInstance('testLucene', 'en')->getLucene();
sfLucene::getInstance('testLucene', 'fr')->getLucene();
// build invalid indexes structures
file_put_contents($root . 'testLucene/en/random_file', 'r@nd()');
mkdir($root . 'testLucene/foo', 0777, true);
file_put_contents($root . 'testLucene/foo/bar', 'foo');
mkdir($root . 'badIndex/en', 0777, true);
file_put_contents($root . 'badIndex/bar', 'foo');
$dirty = sfLuceneToolkit::getDirtyIndexRemains();
$t->ok(in_array($root . 'testLucene/foo', $dirty), '::getDirtyIndexRemains() schedules valid indexes but invalid cultures for deletion');