<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/7/6
 * Time: 上午9:00
 */
define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
require_once(BASE_ROOT_PATH . '/fooder.php');

require_once(BASE_ROOT_PATH . '/helper/ugc_helper.php');

class TestUGC extends PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass()
    {
        Base::run_util();
    }
    public function testSpecialObj()
    {
        $special['share_title'] = '测试';
        $special['share_image'] = "http://192.168.0.200/data/upload/mobile/special/s0/s0_05240528172336404.png";

        $item['type'] = 'images';
        $item['images'][] = "http://192.168.0.200/data/upload/mobile/special/s0/s0_05240528172336404.png";
        $item['title'] = '第一次见到大海';

        $special['items'][] = $item;
    }

    public function testItem()
    {
        $y = 'a:8:{s:11:"home1_title";s:0:"";s:5:"image";s:25:"s67_05483603748979614.png";s:9:"show_type";s:5:"image";s:9:"show_data";s:25:"s67_05483603748979614.png";s:4:"type";s:0:"";s:4:"data";s:0:"";s:5:"title";s:0:"";s:8:"reserved";N;}';
        $x = unserialize($y);
    }
    public function testInitSrv()
    {
        ugc_helper::init_server();
    }
    public function testBonusMake()
    {
        $param["amount"] = 30;
        $param["bless"] = "晚安阿罗裤八团";
        $param["bonus_type"] = 1;
        $param["deadline"] = 1500983744;
        $param["multi"] = false;
        $param["num"] = 15;
        $param["rate"] = 50;
        $param["type"] = "bonus";

        $_SESSION['member_id'] = 39623;
        $_SESSION['member_mobile'] = '13911129867';
        $_SESSION['member_nickname'] = '江南舟子';


        $builder = new ugc\bonus_builder($param);
        $result = $builder->make();
    }
    public function testComment()
    {
        $special_id = 200;
        $mod_comment = Model('ugc_comment');

        for ($i = 0; $i < 20; ++$i)
        {
            $comment_id = $mod_comment->comment($special_id,36490,"TopA");
            $len = mt_rand(5,20);
            for ($j = 0; $j < $len; ++$j) {
                $mod_comment->respond_comment($special_id,$comment_id,36492,"TopRespond");
            }
        }
    }

    public function getTypeList($condition = array(), $pagesize = '', $fields = '*', $order = '', $limit = '')
    {
        return $this->table('bonus_type')->where($condition)->field($fields)->order($order)->limit($limit)->page($pagesize)->select();
    }

    public function getSpCount($special_id)
    {
        return Model()->table('ugc_comment')->where("comment_id=top_id and special_id={$special_id}")->count();
    }
    public function getSpCommentList($special_id,$page = 0,$count = 0,$lock = false)
    {
        return Model()->table('ugc_comment')->field('*')->where("comment_id=top_id and special_id={$special_id}")->order('comment_id desc')->limit(0)->page($page, $count)->lock($lock)->select();
    }
    public function getSpCommentListex($special_id,$pagesize = '')
    {
        return Model()->table('ugc_comment')->field('*')->where("comment_id=top_id and special_id={$special_id}")->order('comment_id desc')->page($pagesize)->lock($lock)->select();
    }

    public function testSubComments()
    {
        $special_id = 200;
        if($special_id < 0) {
            return self::outerr(errcode::ErrParamter,"该专题不存在");
        }
        $comment_id = 16;

        $mod_comment = Model('ugc_comment');
        $count = $mod_comment->comment_counts($special_id,$comment_id);
        $items = $mod_comment->getSubCommentList($special_id,'comment_id',10,$count);
        $pages = $mod_comment->gettotalpage();
    }

    public function testComments()
    {
        $special_id = 200;
        if($special_id < 0) {
            return self::outerr(errcode::ErrParamter,"该专题不存在");
        }

        $mod_comment = Model('ugc_comment');
        $count = $mod_comment->top_counts($special_id);
        $items = $mod_comment->getTopCommentList($special_id,'comment_id',10,$count);
        $pages = $mod_comment->gettotalpage();

        $mod = Model('ugc_comment');
        $commentids = $this->commentids($items);
        $comments = $mod->getCommentList(array('top_id' => ['in',$commentids]),'*',0);

        $comments_obj =new ugc\comments($comments,$special_id,[]);
        $comments = $comments_obj->comments();
        $users = $comments_obj->users();
    }

    private function comment_list($comments)
    {

    }
    private function commentids($items)
    {
        $result = [];
        foreach ($items as $val) {
            $commentid = intval($val['comment_id']);
            $result[] = $commentid;
        }
        return $result;
    }
    public function testEmojj()
    {
        $mod_comment = Model('ugc_comment');
        $item = $mod_comment->getCommentByID(569);
        $str = $item['content'];
        $s = iconv('UTF-8','UTF-16', $str);

    }

    function compatible_decode($unicode)
    {
        $str = '';
        $arr = array_filter(explode('u', $unicode));
        foreach ($arr as $k => $v) {
            if (strlen($v) == 4) {
                $num = (int)hexdec($v);
                $ord_1 = decbin(0xe0 | ($num >> 12));
                $ord_2 = decbin(0x80 | (($num >> 6) & 0x3f));
                $ord_3 = decbin(0x80 | ($num & 0x3f));
                $str .= chr(bindec($ord_1)) . chr(bindec($ord_2)) . chr(bindec($ord_3));
            }else{
                $str .= chr(hexdec($v));
            }
        }
        return $str;
    }

    public function testU16()
    {
        $x = 'u7f8eu5b9du83b2';
        $y = $this->compatible_decode($x);

        $x = json_decode($x);
        $str = "托尔斯泰🇺🇸🌹😃😄";
        $t = json_encode($str,JSON_UNESCAPED_UNICODE);
        $s = iconv('UTF-16', 'UTF-8', $str);
        $y = iconv_get_encoding();
        $len = iconv_strlen($str,'UTF-8');
    }
    public function testVoteResult()
    {
        $x = 'a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}';
        $y = unserialize($x);
    }
    public function testUpdatevote()
    {
        $mod_special = Model('mb_special');
        $items = $mod_special->field('*')->where(['from_user' => 1,'special_id' => 263])->limit(false)->select();
        foreach ($items as $item)
        {
            $spid = intval($item['special_id']);
            $params      = $item['params'];
            $vote_result = $item['vote_result'];

            if(!empty($params) && !empty($vote_result))
            {
                $params      = unserialize($params);
                $vresult = unserialize($vote_result);
                $vote = $params['vote'];
                if(!empty($vote))
                {
                    $params['vote'] = [];
                    $params['vote'][] = $vote;
                    $vote_result = [];
                    $vote_result[] = $vresult;

                    $mod_special->editUserSpecial($spid,['special_id' =>$spid],['params' => serialize($params),'vote_result' => serialize($vote_result)]);
                }
            }
        }
    }

    public function testParams() {
        $params = 'a:2:{s:4:"vote";a:1:{i:0;a:1:{i:0;a:4:{s:7:"options";a:4:{i:0;s:4:"1号";i:1;s:4:"2号";i:2;s:4:"3号";i:3;s:4:"4号";}s:8:"deadline";i:0;s:9:"vote_type";i:0;s:11:"vote_single";b:0;}}}s:9:"questions";a:0:{}}';
        $params = unserialize($params);

        $vote = $params['vote'][0][0];
        $params['vote'] = [];
        $params['vote'][] = $vote;
        $params = serialize($params);
    }

    public function testTitlexx() {
        $title = '默默地看着就
默默你你你
呃呃呃额额';
        $title = str_replace("\n","\\n",$title);
        $txt = str_replace( array( "\n", "\r" ), array( "\\n", "\\r" ), $title );
    }

    public function testImage()
    {
        $image = 'http://lrlz-image.oss-cn-shenzhen.aliyuncs.com/2792935C-A6BF-4F3C-9771-CF3977C044B3.jpeg?828,1472';
        $regxp = '/\?(\d+),(\d+)/i';
        $val = preg_match($regxp,$image,$match);
        if($val > 0) {
            $width = intval($match[1]);
            $height = intval($match[2]);
        }
    }
    public function testUpload()
    {
        $img_content = $this->data();
        $regx = '/^(data:\s*image\/(\w+);base64,)/';
        if (preg_match($regx, $img_content, $result)) 
        {
            $file_type = $result[2];
            $img = base64_decode( str_replace( $result[1], '', $img_content ) );
            return $img;
        } else {
            return false;
        }
    }

    public function testAsyncUgcPublish()
    {
        account_helper::onAsyncUgcPublish(288);
    }
    public function testAsyncUgcDel()
    {
        account_helper::onAsyncUgcDel(288);
    }
    public function testAsyncUgcSubmit()
    {
        account_helper::onAsyncUgcSubmit(290,39646);
    }
    public function testUrlencode()
    {
        $title = util::base64url_encode('校园招聘福利');
        $x = util::base64url_decode('5qCh5Zut5o6o5bm_');
    }
}


